Android conversion program crashes on button click - android

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

Related

can not resolve getActivity() and getApplicationContext()

I am learning android from Android Tutorial for Beginners 8 # wrap_content, fill_parent, Password Field and Toast in Android
using android studio 2.2.2.
I have done the same as mentioned in tutorial but unable to understand why getApplicationContext() and getActivity methods are not resolvable.
I have tried getActivity(), getActivity().getApplicationContext(), MainActivity.this.... etc. But Toast.makeText method unable to recognize Context.
Can anyone please tell me what is the exact issue? and how can I resolve it?
Below is my code:
package com.example.programingknowledge.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText pass_word;
private Button btn_sbm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
pass_word = (EditText) findViewById(R.id.editText);
btn_sbm = (Button) findViewById(R.id.button);
final CharSequence passText = pass_word.getText().toString();
btn_sbm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), passText, Toast.LENGTH_SHORT).show();
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.programingknowledge.myfirstapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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>
</application>
</manifest>
activity_main.xml
<?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:id="#+id/activity_main"
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="com.example.programingknowledge.myfirstapp.MainActivity"
android:background="#android:color/black">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/editText" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:id="#+id/button"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Just change getApplicationContext() or getContext() to YourActivity.this
For example
Toast.makeText(MainActivity.this, passText, Toast.LENGTH_SHORT).show();
Please do not store context as a field. You are able to get in from any point, without storing.
When you are in Activity that means you have the Context i.e. the Activity. You can achieve this by calling this or MainActivity.this. As you are using the Toast inside the OnClickListener object this will not work here because here this means it will take OnClickLister as its object. So here you must use your class.this i.e MainActivity.this.
In case of Fragment you can achieve context by Calling getActivity() because Fragment must have at least a Activity which is the Context.
And the getApplicationContext() means your whole Application Context.
btn_sbm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, passText, Toast.LENGTH_SHORT).show();
}
});
In your case, you're trying to show a toast from a callback. You should pass the MainActivity.this as context in the makeToast method:
Toast.makeText(MainActivity.this, passText, Toast.LENGTH_SHORT).show();
btn_sbm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), passText, Toast.LENGTH_SHORT).show();
}
});
Try this
Please, post your AndroidManifest.xml and activity_main.xml files.
Upd.: The problem is that in your app's build.gradle file was compileSdkVersion 25, but you don't download it. Also you should always download android-sdk sources according to your targetSdkversion. So, just keep your Android Studio files current and that problems should go away.
Toast.makeText(MainActivity.this, passText, Toast.LENGTH_SHORT).show();
The above should work. If not, post your Logcat for the crash.

How to send the data from the current activity to the previous activity?

I've created two activities. The first accepts the name of a student through an edit text and has a submit button. When the submit button is pressed, the next activity opens which has an edit text (to accept the marks) along with a back button.
I wish to return the marks entered to the first activity, when the back button of second activity is pressed so that the marks are displayed in the first edit text.
Kindly provide me with the code for the two activities.
You can use activity for result . An example is given here
Hello Akanksha Gahalout, if i understand your question, you want to pass Data between two activities and vice versa , if so, you should use Intent and use PutExtrat() method to pass data from one Activity to another,then you can recuperate data from the second Activity with using Bundle and getExtras() method.
Here's an example :
FirstActivity.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="${relativePackage}.${activityClass}" >
<EditText
android:id="#+id/EditTextFirstActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/StudentName"
android:ellipsize="start"
android:gravity="center_horizontal"
android:labelFor="#+id/EditText1" />
<Button
android:id="#+id/buttonFirstActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/OK" />
<TextView
android:id="#+id/TextViewFirstActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#000000"
android:ellipsize="start"
android:gravity="center_horizontal"
android:textSize="20sp" />
</LinearLayout>
FirstActivity.java :
package dz.A;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class FirstActivity extends Activity implements OnClickListener {
private TextView textView;
private EditText editTextFirstActivity;
private Button buttonFirstActivity;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
editTextFirstActivity = (EditText) findViewById(R.id.EditTextFirstActivity);
textView = (TextView) findViewById(R.id.TextViewFirstActivity);
buttonFirstActivity = (Button) findViewById(R.id.buttonFirstActivity);
buttonFirstActivity.setOnClickListener(this);
Intent iin = getIntent();
Bundle b = iin.getExtras();
if (b != null) {
String marks = (String) b.get("STUDENT_MARKS");
textView.setText(marks);
}
}
#Override
public void onClick(View v) {
if (v == buttonFirstActivity) {
Log.i("OK", "onClickOK");
Intent i = new Intent(this, SecondActivity.class);
Log.i("OK", "IntentOK");
Log.i("OK", "startActivity(i)OK");
String studentName = editTextFirstActivity.getText().toString()
.trim();
Log.i("OK", "studentNameOK : " + studentName);
i.putExtra("STUDENT_NAME", "Student Name : " + studentName);
Log.i("OK", "i.putExtraOK");
startActivity(i);
finish();
Log.i("OK", "finish()OK");
}
}
}
SecondActivity.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/EditTextSecondActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="start"
android:gravity="center_horizontal"
android:hint="#string/Marks"
android:inputType="number"
android:labelFor="#+id/EditText1" />
<Button
android:id="#+id/buttonSecondActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/sendMarks" />
<TextView
android:id="#+id/TextViewSecondActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
SecondActivity.java :
package dz.A;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends Activity implements OnClickListener {
private TextView textViewSecondActivity;
private EditText editTextSecondActivity;
private Button buttonSecondActivity;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
textViewSecondActivity = (TextView) findViewById(R.id.TextViewSecondActivity);
editTextSecondActivity = (EditText) findViewById(R.id.EditTextSecondActivity);
buttonSecondActivity = (Button) findViewById(R.id.buttonSecondActivity);
buttonSecondActivity.setOnClickListener(this);
Intent iin = getIntent();
Bundle b = iin.getExtras();
if (b != null) {
String studentName = (String) b.get("STUDENT_NAME");
textViewSecondActivity.setText(studentName);
}
}
#Override
public void onClick(View v) {
if (v == buttonSecondActivity) {
String marks = editTextSecondActivity.getText().toString().trim();
Intent i = new Intent(this, FirstActivity.class);
i.putExtra("STUDENT_MARKS", "Student Marks : " + marks);
startActivity(i);
finish();
}
}
}
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dz.A"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="dz.A.FirstActivity"
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="dz.A.SecondActivity"/>
</application>
</manifest>
String.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Project</string>
<string name="hello_world">Hello world!</string>
<string name="OK">OK</string>
<string name="Marks">Marks</string>
<string name="sendMarks">sendMarks</string>
<string name="StudentName">StudentName</string>
</resources>
I hope that helps you.

Blank Activity ( Android ) waiting for debugger

I've got the "waiting for debugger to attach" message and a blank activity that nothing happen for whatever time im waiting!I'm trying to do a simple main activity that shows a question and "+" is the true and "-" is the false. Like true-false game.
here is my manigest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.calculator"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.SET_DEBUG_APP"></uses-permission>
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:debuggable="true"
>
<activity
android:name="com.example.calculator.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>
here is my java :
package com.example.calculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements android.view.View.OnClickListener {
Button button;
Button button2;
Button button3;
TextView text;
String[] questions ;
boolean[] answers;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
text = (TextView) findViewById(R.id.textView1) ;
questions[0]="Are u hungry?";
answers[0]=true;
text.setText(questions[0]);
button.setOnClickListener(this);
button2.setOnClickListener(this);
}
public void onClick(View v) {
//parsing should be here
switch(v.getId()){
case R.id.button1:
if( answers[0]==true){
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Your result:");
alertDialog.setMessage("True !Congratulations");
alertDialog.show();}
else{ AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Your result:");
alertDialog.setMessage("false !You are idiot!");
alertDialog.show();}
break;
case R.id.button2:
if( answers[0]==false){
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Your result:");
alertDialog.setMessage("Correct !Congratulations");
alertDialog.show();}
else{ AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Your result:");
alertDialog.setMessage("Incorrect !You are idiot!");
alertDialog.show();}
break;
}
}
}
here is my xml
<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"
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=".MainActivity"
android:background="#99FFFF">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:text="#string/num1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="#FFFFFF"/>
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/button1"
android:text="#string/minus"
android:background="#FFFFFF"/>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button2"
android:background="#FFFFFF"
android:text="#string/add" />
</RelativeLayout>
You're declaring your variables as arrays
String[] questions ;
boolean[] answers;
and then you're using them without initializing them, so java doesnt't know the size of the arrays.
questions[0]="Are u hungry?";
answers[0]=true;
One possible solution is defining the size of the array:
String[] questions = new String[10]; // define the size of the array
Another solution would be using an ArrayList.

Android application stopped

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.

An error during instantiate intent

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.........

Categories

Resources