i was trying to connect a database that i created using SQLite. i dont have any errors, but whenever i try to run the app on emulator, the application automatically stops working as soon as it starts.. heres the code which has only one activity that connects database and extract data from it and displays it
MANIFEST :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sampledb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.sampledb.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
heres the layout file that has a textview and a radio group in which the text is extraxted from database:
XML : In the following XML file i have created a text view where i would like to put the question and radio buttons and its texts where i would like to put choices. heres the code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainScreen" >
<TextView
android:id="#+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sample" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<RadioGroup
android:id="#+id/rg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
/>
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
</LinearLayout>
<TextView
android:id="#+id/explanation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Explanation" />
</LinearLayout>
In the Database i have few coloumns that has a question and few choices which i tried to use it as a text for radiobuttons.
the activity code :
package com.example.sampledb;
import java.util.Locale;
import com.example.sampledb.R;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView Question;
RadioGroup G1;
RadioButton B1, B2, B3, B4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Question= (TextView)findViewById(R.id.question);
G1=(RadioGroup)findViewById(R.id.rg1);
B1=(RadioButton)findViewById(R.id.radio0);
B2=(RadioButton)findViewById(R.id.radio1);
B3=(RadioButton)findViewById(R.id.radio2);
B4=(RadioButton)findViewById(R.id.radio3);
SQLiteDatabase db;
db = openOrCreateDatabase("vocablearner.db",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
Cursor cur = db.query("MainTable", null, null, null, null, null,null);
if (cur != null ) {
if (cur.moveToFirst()) {
do {
String Quest = cur.getString(cur.getColumnIndex("_id"));
String ans1 = cur.getString(cur.getColumnIndex("C1"));
String ans2 = cur.getString(cur.getColumnIndex("C2"));
String ans3 = cur.getString(cur.getColumnIndex("C3"));
String ans4 = cur.getString(cur.getColumnIndex("C4"));
//String crt = cur.getString(cur.getColumnIndex("Ccrt"));
Question.setText(Quest);
B1.setText(ans1);
B2.setText(ans2);
B3.setText(ans3);
B4.setText(ans4);
}while (cur.moveToNext()); } } }
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} } `
Here i have used a method openorcreatedatabase to define the database that i saved in assets folder and i have used cursor to extract the data.
In database file,the _id is made as primary key ( the questions ) and the coloumns c1 c2 c3 c4 are answers to that.
what changes should i make to run this code? Should i upload the database contents too?? and is there any other ways to use the database in android?
Here i have used a method openorcreatedatabase to define the database that i saved in assets folder and i have used cursor to extract the data.
In database file,the _id is made as primary key ( the questions ) and the coloumns c1 c2 c3 c4 are answers to that.
what changes should i make to run this code? Should i upload the database contents too?? and is there any other ways to use the database in android?
Check if you need to add any permissions in your manifest. Like writing to your external memory.
Related
I am a noob at this, so going to keep it short.
This is a practice app which sends data from first activity to second activity as a message and then the first activity receives data from the second activity as a message. The tutorial I'm following instructed me to use the StartActivityForResult() function to extract data from a second activity to the first activity. I have two questions:
What is wrong with this code:
I. The MainActivity.java file
package com.example.android.twoactivitiesredo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
//LOG_TAG contains the name of the class package, encapsulated for ease
public static final String EXTRA_MESSAGE =
"com.example.android.twoactivities.extra.MESSAGE";
//This will be used as the unique key to send data to the second Activity
public static final int TEXT_REQUEST = 1;
//This is used to define the key for a particular type of response that
you're interested in.
private EditText mMessageEditText;
//This EditText is used to send the message to the second activity
private TextView mReplyHeadTextView;
private TextView mReplyTextView;
//These private variables hold the reply header and the reply TextViews
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMessageEditText = (EditText) findViewById(R.id.editText_main);
mReplyHeadTextView = (TextView) findViewById(R.id.textHeaderReply);
mReplyTextView = (TextView) findViewById(R.id.textMessageReply);
}
public void launchSecondActivity(View view) {
Log.d(LOG_TAG, "Button Clicked!");
Intent intent = new Intent(this, SecondActivity.class);
String message = mMessageEditText.getText().toString();
//Gets the data from EditText, converts it to String, and stores it in message
intent.putExtra(EXTRA_MESSAGE,message);
startActivityForResult(intent, TEXT_REQUEST);
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == TEXT_REQUEST){
if(resultCode == RESULT_OK){
String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY);
mReplyHeadTextView.setVisibility(View.VISIBLE);
mReplyTextView.setText(reply);
mReplyTextView.setVisibility(View.VISIBLE);
}
}
}
}
II. The SecondActivity.java file
package com.example.android.twoactivitiesredo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity{
public static final String EXTRA_REPLY =
"com.example.android.twoactivities.extra.REPLY";
//This tag will be used as a key to send reply to first activity
private EditText mReply;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
//Gets the intent that activated this activity
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//This extracts the extra text sent along with this key String, thus
receiving the text
TextView textView = (TextView) findViewById(R.id.text_message);
textView.setText(message);
mReply = (EditText) findViewById(R.id.replyEditText);
//Do not reuse the intent from the first activity, create a new one
Intent replyIntent = new Intent();
setResult(RESULT_OK,replyIntent);
//RESULT_OK has the value -1 and is used as a Result code in the Activity
class
//to check that the data is send without a complication
finish();
}
public void returnReply(View view) {
}
}
III. The activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_main"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="#string/sendButton"
android:onClick="launchSecondActivity"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText_main"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toStartOf="#id/button_main"
android:layout_toLeftOf="#+id/button_main"
android:hint="Enter Your Message Here"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textHeaderReply"
android:text="#string/textHeaderReply"
android:visibility="invisible"/>
<!--The visibility mode is used to select how the attribute will be before data is passed to it.-->
<!--Invisible means that the attribute will be invisible before data is passed to it, hence not confusing the user-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textMessageReply"
android:layout_below="#+id/textHeaderReply"
android:visibility="invisible"/>
IV. The activity_second.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_header"
android:layout_marginBottom="20dp"
android:text="#string/text_header"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_main"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="#string/sendButton"
android:onClick="launchSecondActivity"/>
<TextView
android:id="#+id/text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text_header"
android:layout_margin="10dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/replyEditText"
android:onClick="returnReply"
android:layout_toLeftOf="#+id/button_main"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:hint="#string/replyText"
/>
</RelativeLayout>
V. The AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.twoactivitiesredo">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/secondActivity"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.twoactivitiesredo.MainActivity"/>
</activity>
</application>
</manifest>
I am sorry if the code is taking a lot of space, but I really needed help in clarifying my doubts and problems here, about why this code isn't working.
Thanks in advance to all the answers.
Replace your secondActivity.java with this:
package com.example.android.twoactivitiesredo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity{
public static final String EXTRA_REPLY =
"com.example.android.twoactivities.extra.REPLY";
//This tag will be used as a key to send reply to first activity
private EditText mReply;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.text_message);
textView.setText(message);
mReply = (EditText) findViewById(R.id.replyEditText);
}
public void returnReply(View view) {
Intent replyIntent = new Intent();
String replyMessage = mReply.getText().toString();
replyIntent.putExtra(EXTRA_REPLY, replyMessage);
setResult(RESULT_OK,replyIntent);
finish();
}
}
In activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_header"
android:layout_marginBottom="20dp"
android:text="#string/text_header"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_main"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="Reply Button"
android:onClick="returnReply"
/>
<TextView
android:id="#+id/text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text_header"
android:layout_margin="10dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/replyEditText"
android:layout_toLeftOf="#+id/button_main"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:hint="#string/replyText"
/>
</RelativeLayout>
I've implemented a sliding menu android framework by following a tutorial video, the menu works well but I can't figure out how to make the buttons generate new "fresh", i.e. distinct, pages wherein I can place subsequent components/"activities".
Right now all the buttons do is "toggle" back and forth between having the menu exposed, and hiding the menu, making visible the complete "landing page".
This is how it looks:
In my layout file, I think this is responsible for assigning functionality to the buttons:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 3" />
So as you can see the buttons evoke the method toggleMenu which looks like this:
public void toggleMenu() {
switch (this.menuCurrentState){
case CLOSED:
this.menuCurrentState = MenuState.OPENING;
this.menu.setVisibility(View.VISIBLE);
this.menuAnimatonScroller.startScroll(0, 0, this.getMenuWidth(),
0, menuAnimationDuration);
break;
case OPEN:
this.menuCurrentState = MenuState.CLOSING;
this.menuAnimatonScroller.startScroll(this.currentContentOffset,
0, -this.currentContentOffset, 0, menuAnimationDuration);
break;
default:
return;
}
this.menuAnimationHandler.postDelayed(this.menuAnimationRunnable, menuAnimationPollingInterval);
this.invalidate();
}
I guess I need to make a new method for generating fresh pages and then assign that to the button push rather than toggle menu, I've tried this a few times using intent but I've not been able to quite figure it out.
What do I need to account for in such an operation?
How should such a function look?
The complete code can be found on my github page.
Thank you for your consideration.
dig this tutorial, it's one of the only helpful ones, in my opinion:
http://www.mkyong.com/android/android-activity-from-one-screen-to-another-screen/
or check the pertinent info below"
XML Layouts
Create following two XML layout files in “res/layout/” folder :
res/layout/main.xml – Represent screen 1
res/layout/main2.xml – Represent screen 2
File : res/layout/main.xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm screen 1 (main.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me to another screen" />
File : res/layout/main2.xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm screen 2 (main2.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
2. Activities
Create two activity classes :
AppActivity.java –> main.xml
App2Activity.java –> main2.xml
To navigate from one screen to another screen, use following code :
Intent intent = new Intent(context, anotherActivity.class);
startActivity(intent);
File : AppActivity.java
package com.mkyong.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class AppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, App2Activity.class);
startActivity(intent);
}
});
}
}
File : App2Activity.java
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
3. AndroidManifest.xml
Declares above two activity classes in AndroidManifest.xml.
File : AndroidManifest.xml
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".AppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".App2Activity" >
</activity>
</application>
Here are my files (Conversion.xml, Conversion.java and AndroidManifest.xml). It is a small part of a project. I am able to get to the screen where I want to convert the power in watts to decibels. But when I enter the power value and click the button "Convert", the app crashes and goes back to the my app home screen.
conversion.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ConvertTodB" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter the power in Watts"
android:inputType="number"
android:text="#string/et1" />
<Button
android:id="#+id/bConvert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Convert" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Result in dB"
android:text="#string/et2"
android:textSize="20sp"/>
</LinearLayout>
Conversion.java
package com.example.rfconcepts;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.Math;
public class Conversion extends Activity {
EditText entered_val;
TextView result_val;
Button bCon;
#Override
protected void onCreate(Bundle convertTodB) {
super.onCreate(convertTodB);
setContentView(R.layout.conversion);
entered_val = (EditText) findViewById(R.string.et1);
bCon = (Button) findViewById(R.id.bConvert);
result_val = (TextView) findViewById(R.string.et2);
bCon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(entered_val.getText() != null && entered_val.getText().length() != 0){
result_val.setText(String.valueOf(10 * Math.log10(Double
.valueOf(entered_val.getText().toString()))));
}
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rfconcepts"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.rfconcepts.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.rfconcepts.Conversion"
android:label="#string/app_name">
</activity>
</application>
</manifest>
I thank you in advance for the advise.
P.S:
Here is a list of things I did after reading a lot of solutions for various questions posted here.
Instead of using onClickListener, in conversion.xml, i added the attribute "android:onClick = "onClickConvert" and added the follwoing onClickConvert method in the Activity class.
public void onClickConvert(View v){
if(entered_val.getText() != null && entered_val.getText().length() != 0){
result_val.setText(String.valueOf(10 * Math.log10(Double
.valueOf(entered_val.getText().toString()))));
}
}
Changed Double.parseDouble() to Double.valueOf() (Although, I do not think this is the issue)
Checked if the editText itself has null, hence the if statement.
entered_val = (EditText) findViewById(R.string.et1);
Why are you referring to a string? It should be an ID.
You did not declare an ID for your EditTexts. Do something like this:
<EditText
[...]
android:id="#+id/MyEditText"
/>
Then you can refer to it like this:
entered_val = (EditText) findViewById(R.Id.MyEditText);
Also do the same with your TextView.
Second issue:
EditText.getText() returns an Editable, but you need a String. Convert it like this:
if(entered_val.getText().toString() != null && entered_val.getText().toString().length() != 0
You shall an ID in EditText of your conversion.xml and then access it in Java code. Post your logcat error for more concrete answer
you need to add reference id to the EDITTEXT in power and result
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ConvertTodB" >
<EditText
android:id="#+id/et1" // add the reference id
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter the power in Watts"
android:inputType="number"
android:text="#string/et1" />
<Button
android:id="#+id/bConvert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Convert" />
<TextView
android:id="#+id/et2"// add the reference id
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Result in dB"
android:text="#string/et2"
android:textSize="20sp"/>
Also wired up those edittext with referenceid in your java code
entered_val = (EditText) findViewById(R.id.et1); // wired up with the xml id of the component
bCon = (Button) findViewById(R.id.bConvert);
result_val = (TextView) findViewById(R.id.et2);// wired up with the xml id of the component
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
First- sorry if my question a bit dumb, I'm totally new to Android development ...
I'm trying to start a new activity from my "LAUNCHER" activity and run it on the emulator, but every time I click on the "Next" button (which should start my second activity), I get an error msg saying: "unfortunately, Omis hang man free has stopped", and then closes the app...
also, when I re-launch the same app in the emulator without re-installing it from Eclipse, it doesn't even showing me the first screen...
I don't even know where my problem lies, so I attached 5 codes:
1'st activity layout (named "activity_open")
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".OpenActivity"
android:background="#DCDCDC" >
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_weight="30" >
<TextView
android:id="#+id/tvEnterName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="#string/stEnterName"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_weight="40" >
<EditText
android:id="#+id/etNameInput"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="#string/stHintNameEnter"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_weight="30" >
<Button
android:id="#+id/bNext"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/stNext"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
1'st activity class (named "OpenActivity")
package com.omi.hangmanfree;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class OpenActivity extends Activity {
TextView tvEnterYourName;
EditText etName;
Button bContinue;
String myName, tmp;
final String errorStr = "\nYou must enter a name!";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open);
initialize();
bContinue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
myName = etName.getText().toString();
if(etName.getText().toString().equals("") == true)
{
tvEnterYourName.setText(tmp + errorStr);
tvEnterYourName.setTextColor(Color.RED);
}
else
{
try {
Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY");
startActivity(gameIntent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
private void initialize() {
// TODO Auto-generated method stub
tvEnterYourName = (TextView)findViewById(R.id.tvEnterName);
etName = (EditText)findViewById(R.id.etNameInput);
bContinue = (Button)findViewById(R.id.bNext);
myName = "~";
tmp = tvEnterYourName.getText().toString();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_open, menu);
return true;
}
}
2'nd activity class (named "GameActivity")
package com.omi.hangmanfree;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.GridView;
import android.widget.TextView;
public class GameActivity extends Activity {
TextView tt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
GridView gvGrid = (GridView)findViewById(R.id.gvHagdara);
tt.setText("_");
gvGrid.addView(tt);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_game, menu);
return true;
}
}
2'nd activity layout (named "layout_game")
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GameActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<GridView
android:id="#+id/gvHagdara"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/textView1"
android:layout_centerInParent="false"
android:numColumns="auto_fit" >
</GridView>
</RelativeLayout>
5. manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.omi.hangmanfree"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".OpenActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GameActivity"
android:label="#string/title_activity_game" >
<intent-filter>
<action android:name="com.omi.hangmanfree.GAMEACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I'm not looking for code improvements (I am only learning the language), so please do not send me suggestions to improve the code (I know there are a lot!) --- I just want this code to work.
The problem may be in the Intent. Try this:
Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class);
Please Remove finish(); from onPause() Method from OpenActivity and remove gvGrid.addView(tt); from GameActivity because gridview is not container layout and write below code
Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class);
startActivity(gameIntent);
instead of
Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY");
startActivity(gameIntent);
it will solve your problem.
please replace in openActivity
Intent gameIntent = new Intent(getApplicationContext(), GameActivity.class);
startActivity(gameIntent);
Try changing here:
Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY");
startActivity(gameIntent);
to
Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class);
startActivity(gameIntent);
I am totally new to android programming and I was reading a book called "Hello Android"
Basically the book teaches us Android by using a Sudoku game example.
Here is the main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/background"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:padding="30dip"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center" >
<TextView
android:text="#string/main_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp" />
<Button
android:id="#+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/continue_label" />
<Button
android:id="#+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/new_game_label" />
<Button
android:id="#+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/about_label" />
<Button
android:id="#+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exit_label" />
</LinearLayout>
</LinearLayout>
Here is the string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Sudoku</string>
<string name="main_title">Android Sudoku</string>
<string name="continue_label">Continue</string>
<string name="new_game_label">New Game</string>
<string name="about_label">About</string>
<string name="exit_label">Exit</string>
<color name="background">#3500ffff</color>
<string name="about_title">About Android Sudoku</string>
<string name="about_text">\
Sudoku is a logic-based number placement puzzle.
Starting with a partially completed 9x9 grid, the
objective is to fill the grid so that each
row, each column, and each of the 3x3 boxes
(also called <i>blocks</i>) contains the digits
1 to 9 exactly once.
</string>
</resources>
Here is the additional manifest to the old:
<activity android:name=".About"
android:label="#string/about_title" >
</activity>
Here is the about.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip" >
<TextView
android:id="#+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about_text" />
</ScrollView>
And finally the Sudoku.java
package org.example.sudoku;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
public class Sudoku extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener((OnClickListener) continueButton);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener((OnClickListener) this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener((OnClickListener) this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class); /** Here is the Error **/
startActivity(i);
break;
// More buttons go here (if any) ...
}
}
}
Let me explain it a little bit.
The Sudoku main page consists of 4 buttons, the book is teaching us to implement the button, so when the user presses it, the program will direct the user to the about page (which is just a page of text).
The error happened on the About.class, Eclipse said that there is no such class called About. And I am not quite understand why there is an About.class in the intent argument as well....
And idea??
you have class About.java in org.example.sudoku package?
public class About extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}
Did you write down your activity in the manifest, using the right or same package as your other activity?
http://developer.android.com/guide/topics/manifest/manifest-intro.html
Could you also please post your log cat?
as per your comment ........ You must have the file About.java in you project and also add it's entry in the manifest...
<activity android:name=".About"
android:label="#string/app_name">
</activity>
so add About.java in parallel to Sudoku.java in your project and it also need to extends the Activity.........