I am able to compile, build, and install the project successfully And the project is running well in portrait, But In landscape mode, When I click in button, the app says: Unfortunately has stopped. Could you please help me figure this out?
This is The app Logcat In Landscape mode:
01-24 09:58:19.936: W/dalvikvm(6847): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-24 09:58:19.986: E/AndroidRuntime(6847): FATAL EXCEPTION: main
01-24 09:58:19.986: E/AndroidRuntime(6847): java.lang.NullPointerException
01-24 09:58:19.986: E/AndroidRuntime(6847): at com.Divani.Marzieh.ExamActivity.addItemList(ExamActivity.java:79)
01-24 09:58:19.986: E/AndroidRuntime(6847): at com.Divani.Marzieh.ExamActivity$1.onClick(ExamActivity.java:71)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.view.View.performClick(View.java:3511)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.view.View$PerformClick.run(View.java:14105)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.os.Handler.handleCallback(Handler.java:605)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.os.Handler.dispatchMessage(Handler.java:92)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.os.Looper.loop(Looper.java:137)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-24 09:58:19.986: E/AndroidRuntime(6847): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 09:58:19.986: E/AndroidRuntime(6847): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 09:58:19.986: E/AndroidRuntime(6847): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-24 09:58:19.986: E/AndroidRuntime(6847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-24 09:58:19.986: E/AndroidRuntime(6847): at dalvik.system.NativeStart.main(Native Method)
This is The examActivity Code:
public class ExamActivity extends Activity {
private EditText etInput1;
private EditText etInput2;
private Button btnAdd;
private ListView lvItem;
private ArrayList<Item> itemArrey;
private ArrayAdapter<Item> itemAdapter;
private static TabHost tabHost;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost=(TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("Tab 1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("LIST");
TabSpec spec2=tabHost.newTabSpec("Tab 2");
spec2.setIndicator("DETAILS");
spec2.setContent(R.id.tab2);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.setCurrentTab(id.tab1);
setUpView();
}
public static TabHost getCurrentTabHost(){
return tabHost;
}
private void setUpView() {
// TODO Auto-generated method stub
etInput1 = (EditText)this.findViewById(R.id.editText1);
etInput2 = (EditText)this.findViewById(R.id.editText2);
btnAdd = (Button)this.findViewById(R.id.button1);
lvItem = (ListView)this.findViewById(R.id.list);
itemArrey = new ArrayList<Item>();
itemArrey.clear();
itemAdapter = new CustomlistActivity(this, android.R.layout.simple_list_item_1,R.id.textView1,itemArrey);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addItemList();
}
});
}
protected void addItemList() {
// TODO Auto-generated method stub
if (isInputValid(etInput1) && isInputValid(etInput2)) {
if(((RadioButton)findViewById(R.id.radio0)).isChecked())
itemArrey.add(new Item(R.drawable.t,etInput1.getText().toString()+"\n"+etInput2.getText().toString()));
else if(((RadioButton)findViewById(R.id.radio1)).isChecked())
itemArrey.add(new Item(R.drawable.s,etInput1.getText().toString()+"\n"+etInput2.getText().toString()));
else if(((RadioButton)findViewById(R.id.radio2)).isChecked())
itemArrey.add(new Item(R.drawable.d,etInput1.getText().toString()+"\n"+etInput2.getText().toString()));
itemAdapter.notifyDataSetChanged();
ExamActivity.getCurrentTabHost().setCurrentTab(0);
}
}
protected boolean isInputValid(EditText etInput2) {
// TODO Auto-generatd method stub
if (etInput2.getText().toString().trim().length()<1) {
etInput2.setError("Please Enter Item");
return false;
} else {
return true;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater .inflate(R.menu.mymenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
EditText e = (EditText)findViewById(R.id.editText3);
switch (item.getItemId()) {
case R.id.item1:
Toast toast = Toast.makeText(ExamActivity.this, e.getText().toString(), 5000);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
whenever you change the orientation of the screen the activity gets restarted after that the onStart() method is called.
Initialize your code that you are using at ExamActivity.addItemList(ExamActivity.java:79) in your onStart() method ex: your list or array
go through the site http://www.vogella.com/tutorials/AndroidLifeCycle/article.html#configurationchange
Do you have a separate layout main.xml for landscape, ie layout/main.xml and layout-land/main.xml ? If so then check that layout-land/main.xml is not missing some or all of the radio buttons radio0/1/2.
Related
I write this counter but when I lunch the app get crashed I don't now where the error some help and thank you tell where is the Error here or how can I do code in right way
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
TextView txt = (TextView)findViewById(R.id.textView);
count i;
public void bu1(View view) {
starrtime();
}
public void bu2(View view) {
i.cancel();
}
void starrtime(){
i = new count(100,1000);
i.start();
}
public class count extends CountDownTimer {
public count(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
txt.setText(String.valueOf(millisUntilFinished));
}
#Override
public void onFinish() {
txt.setText("Done");
}
}
}
this my log cat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kira.counter, PID: 4598
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.kira.counter/com.example.kira.counter.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:116)
at android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:147)
at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:27)
at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:50)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.kira.counter.MainActivity.(MainActivity.java:23)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
I believe you can not initialize a TextView before runtime because the views are not set yet. Classes should be capitalized. Try this.
public class MainActivity extends AppCompatActivity{
private TextView txt;
private Count i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)findViewById(R.id.textView);
}
}
I am trying to pass String data using intent from Authorites.class to Issues.class . When a radio button is chosen and button is clicked, 1st activity has to show toast as well as pass the Radio button value to next activity.
When i choose the radio-button and press the button in Authorites.class,the app stops and exits. Plz find where have i gone wrong
public class Authorities extends AppCompatActivity {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
//private GoogleApiClient client;
RadioButton auth_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_authorities);
final RadioGroup auth_grp = (RadioGroup) findViewById(R.id.rgrp);
Button button1 = (Button) findViewById(R.id.authselect);
button1.setOnClickListener( new View.OnClickListener(){
#Override
public void onClick (View v){
// store the text corresponding to the RadioButton which is clicke
int sa = auth_grp.getCheckedRadioButtonId();
auth_button = (RadioButton) findViewById(sa);
// String auth=auth_button.getText().toString();
Toast.makeText(Authorities.this, auth_button.getText(), Toast.LENGTH_SHORT).show();
Intent i;
i=new Intent(Authorities.this, Issues.class);
// Intent i = new Intent().setClassName("com.example.chethan.wapp.Authorities", "com.example.chethan.wapp.Issues");
// i.putExtra("Auth",auth);
// Starts TargetActivity
// Authorities.this.startActivity(i);
startActivity(i);
}
});
}
Issues.class should recieve the Intent data returned from previous activity
public class Issues extends AppCompatActivity {
CheckBox mws,sl,swc,sws,dr;
Button b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// mws=(CheckBox)findViewById(R.id.mws);
// sl=(CheckBox)findViewById(R.id.sl);
// swc=(CheckBox)findViewById(R.id.swc);
// sws=(CheckBox)findViewById(R.id.sws);
// dr=(CheckBox)findViewById(R.id.dr);
b2=(Button)findViewById(R.id.nxt);
Bundle extras = getIntent().getExtras();
String value1 = extras.getString("Auth");
Toast.makeText(getApplicationContext(),"Authority you chose is:\n"+value1,Toast.LENGTH_LONG).show();
}
Here's the log cat:
02-29 10:54:00.594 1960-1960/com.example.chethan.wapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chethan.wapp, PID: 1960
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chethan.wapp/com.example.chethan.wapp.Issues}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.chethan.wapp.Issues.onCreate(Issues.java:34)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Because you are getting string value1 data from getString("Auth") of Intent in Issues class but in Authorities class you are not putting any ("Auth") value in Intent obj.
So uncomment this line...
i.putExtra("Auth",auth);
then run the project...
I've programmed a game for android, everything works fine, but now I want my app to have Google play Games services (leaderboards and achievements). I used the Google example code to log in to the Google services (no errors in the script), but every time I want to connect with my App in debug mode, I get this error:
6-29 11:48:29.391 23779-23779/com.JFKGames.theepicbutton E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.JFKGames.theepicbutton, PID: 23779
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=9001, result=10004, data=null} to activity {com.JFKGames.theepicbutton/com.JFKGames.theepicbutton.MainActivity}: java.lang.IllegalStateException: GoogleApiClient must be connected.
at android.app.ActivityThread.deliverResults(ActivityThread.java:3446)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3489)
at android.app.ActivityThread.access$1300(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: GoogleApiClient must be connected.
at com.google.android.gms.internal.fq.a(Unknown Source)
at com.google.android.gms.games.Games.c(Unknown Source)
at com.google.android.gms.games.internal.api.LeaderboardsImpl.submitScore(Unknown Source)
at com.google.android.gms.games.internal.api.LeaderboardsImpl.submitScore(Unknown Source)
at com.JFKGames.theepicbutton.MainActivity.onActivityResult(MainActivity.java:79)
at android.app.Activity.dispatchActivityResult(Activity.java:5446)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3442)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3489)
at android.app.ActivityThread.access$1300(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
And the App crashes. Here's my code for the MainActivity where I want it to connect:
public class MainActivity extends BaseGameActivity implements
GameHelper.GameHelperListener, View.OnClickListener {
public static int REQUEST_LEADERBOARD = 1002;
boolean mExplicitSignOut = false;
boolean mInSignInFlow = false;
GoogleApiClient mClient() {
return null;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
setRequestedClients(BaseGameActivity.CLIENT_GAMES | BaseGameActivity.CLIENT_APPSTATE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.startbutton);
button.setOnClickListener (this);
Button highscorebutton = (Button)findViewById(R.id.highscorebutton);
highscorebutton.setOnClickListener(this);
findViewById(R.id.sign_in_button).setOnClickListener(this);
findViewById(R.id.sign_out_button).setOnClickListener(this);
}
public void onClick(View view) {
if(view.getId()==R.id.startbutton) {
startActivityForResult(new Intent(this, buttonActivity.class), 1);
} else if(view.getId()==R.id.highscorebutton) {
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(), getString(R.string.the_best_players)),REQUEST_LEADERBOARD);
} else if (view.getId() == R.id.sign_in_button) {
// start the asynchronous sign in flow
beginUserInitiatedSignIn();
}
else if (view.getId() == R.id.sign_out_button) {
// sign out.
signOut();
// show sign-in button, hide the sign-out button
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
findViewById(R.id.sign_out_button).setVisibility(View.GONE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Games.Leaderboards.submitScore(getApiClient(), getString(R.string.the_best_players), resultCode);
if(requestCode==1) {
if(resultCode > leseHighscore()) {
schreibeHighscore(resultCode);
}
}
}
#Override
public void onSignInFailed() {
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
findViewById(R.id.sign_out_button).setVisibility(View.GONE);
}
#Override
public void onSignInSucceeded() {
View a = findViewById(R.id.highscorebutton);
a.setVisibility(View.VISIBLE);
View b = findViewById(R.id.button3);
b.setVisibility(View.VISIBLE);
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);
}
}
Thanks, GoogleWelt
According to the official documentation, "Before any operation is executed, the GoogleApiClient must be connected"
When the user in not connected(signed in) and clicks to show leaderboards or achievements, it results in the exception thrown. Modify your code for launching the leaderboard like this:
} else if(view.getId()==R.id.highscorebutton) {
if (isSignedIn())
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(), getString(R.string.the_best_players)), REQUEST_LEADERBOARD);
else showAlert("Please sign in to view leaderboards");
}
Use the same logic for showing achievements:
if (isSignedIn())
startActivityForResult(Games.Achievements.getAchievementsIntent(getApiClient()), REQUEST_ACHIEVEMENT);
else showAlert("Please sign in to view achievements");
Check the part where you are getting ApiClient i.e. getApiClient().
Write the code below to see if GoogleApiClient is Connected or not.
GoogleApiClient mGoogleApiClient;
if(mGoogleApiClient.isConnected()){
// good
}else{
//connect it
mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
}
I m learning android please help me. It gives me following errors in logcat.
02-27 14:14:42.455: D/dalvikvm(1655): GC_FOR_ALLOC freed 46K, 5% free 2891K/3020K, paused 152ms, total 156ms
02-27 14:14:42.465: I/dalvikvm-heap(1655): Grow heap (frag case) to 3.668MB for 810016-byte allocation
02-27 14:14:42.545: D/dalvikvm(1655): GC_FOR_ALLOC freed 2K, 4% free 3680K/3812K, paused 76ms, total 77ms
02-27 14:14:43.425: I/Choreographer(1655): Skipped 35 frames! The application may be doing too much work on its main thread.
02-27 14:14:43.645: D/gralloc_goldfish(1655): Emulator without GPU emulation detected.
02-27 14:14:48.395: I/Choreographer(1655): Skipped 58 frames! The application may be doing too much work on its main thread.
02-27 14:14:49.725: I/Choreographer(1655): Skipped 58 frames! The application may be doing too much work on its main thread.
02-27 14:14:52.355: I/Choreographer(1655): Skipped 61 frames! The application may be doing too much work on its main thread.
02-27 14:14:55.195: D/AndroidRuntime(1655): Shutting down VM
02-27 14:14:55.195: W/dalvikvm(1655): threadid=1: thread exiting with uncaught exception (group=0xb3aaaba8)
02-27 14:14:55.275: E/AndroidRuntime(1655): FATAL EXCEPTION: main
02-27 14:14:55.275: E/AndroidRuntime(1655): Process: com.example.dreamhome, PID: 1655
02-27 14:14:55.275: E/AndroidRuntime(1655): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dreamhome/com.example.dreamhome.LoginFormActivity}: java.lang.NullPointerException
02-27 14:14:55.275: E/AndroidRuntime(1655): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-27 14:14:55.275: E/AndroidRuntime(1655): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-27 14:14:55.275: E/AndroidRuntime(1655): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-27 14:14:55.275: E/AndroidRuntime(1655): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-27 14:14:55.275: E/AndroidRuntime(1655): at android.os.Handler.dispatchMessage(Handler.java:102)
02-27 14:14:55.275: E/AndroidRuntime(1655): at android.os.Looper.loop(Looper.java:136)
02-27 14:14:55.275: E/AndroidRuntime(1655): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-27 14:14:55.275: E/AndroidRuntime(1655): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 14:14:55.275: E/AndroidRuntime(1655): at java.lang.reflect.Method.invoke(Method.java:515)
02-27 14:14:55.275: E/AndroidRuntime(1655): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-27 14:14:55.275: E/AndroidRuntime(1655): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-27 14:14:55.275: E/AndroidRuntime(1655): at dalvik.system.NativeStart.main(Native Method)
02-27 14:14:55.275: E/AndroidRuntime(1655): Caused by: java.lang.NullPointerException
02-27 14:14:55.275: E/AndroidRuntime(1655): at com.example.dreamhome.LoginFormActivity.onCreate(LoginFormActivity.java:45)
02-27 14:14:55.275: E/AndroidRuntime(1655): at android.app.Activity.performCreate(Activity.java:5231)
02-27 14:14:55.275: E/AndroidRuntime(1655): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-27 14:14:55.275: E/AndroidRuntime(1655): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-27 14:14:55.275: E/AndroidRuntime(1655): ... 11 more
02-27 14:15:03.295: I/Process(1655): Sending signal. PID: 1655 SIG: 9
HomeActivity.java
public class HomeActivity extends Activity
{
Button search_property, log_in, exit;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
search_property=(Button)findViewById(R.id.homebutton1);
log_in=(Button)findViewById(R.id.homebutton2);
exit=(Button)findViewById(R.id.homebutton3);
search_property.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent main1=new Intent(HomeActivity.this,EndUserSearchPropertyActivity.class);
startActivity(main1);
}
});
log_in.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent main2 = new Intent(HomeActivity.this,LoginFormActivity.class);
startActivity(main2);
}
});
exit.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
finish();
System.exit(0);
}
});
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}.
*/
private void setupActionBar()
{
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
this is LoginFormActivity.java
public class LoginFormActivity extends Activity
{
private Button sign_up = null;
private Button btnSignIn = null;
LoginDataBaseAdapter loginDataBaseAdapter = null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_form);
// create a instance of SQLite Database
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
final Dialog dialog = new Dialog(LoginFormActivity.this);
// get the Refferences of views
final EditText editTextUserName=(EditText)dialog.findViewById(R.id.login_editText1);
final EditText editTextPassword=(EditText)dialog.findViewById(R.id.login_editText2);
btnSignIn = (Button)dialog.findViewById(R.id.login_form_button1);
// Set On ClickListener
btnSignIn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// get The User name and Password
String userName = editTextUserName.getText().toString();
String password = editTextPassword.getText().toString();
// fetch the Password form database for respective user name
String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);
// check if the Stored password matches with Password entered by user
if(password.equals(storedPassword))
{
Toast.makeText(LoginFormActivity.this, "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
else
{
Toast.makeText(LoginFormActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
}
}
});
dialog.show();
}
#Override
protected void onDestroy()
{
super.onDestroy();
// Close The Database
loginDataBaseAdapter.close();
sign_up = (Button)findViewById(R.id.login_form_button2);
sign_up.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent main2=new Intent(LoginFormActivity.this,SignupFormActivity.class);
startActivity(main2);
}
});
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}.
*/
private void setupActionBar()
{
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login_form, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
final Dialog dialog = new Dialog(LoginFormActivity.this);
Merely instantiating a dialog doesn't inflate/create its layout. All the subsequent dialog.findViewById() calls return null and you'll get the NPE here attempting to call a method on null reference:
btnSignIn = (Button)dialog.findViewById(R.id.login_form_button1);
// Set On ClickListener
btnSignIn.setOnClickListener(new View.OnClickListener()
You probably need to set a content view to your dialog with all the views you want to reference. The views are available with findViewById() after the dialog is showing.
In my android application , I have 3 dialogue boxes in which the user puts info into 3 editTexts and it will display the one of the data onto another class/page after it randomly picks which data to choose.
This is my mainClass
public class MainActivity extends Activity {
//Variables are displayed in this area
String choices[] = new String[3];
//EditText editText;
//EditText editText2;
//EditText editText3;
Button mainButton ;
Random rand = new Random();
int finalChoice;
String displayChoice = "";
EditText editText ;
EditText editText2;
EditText editText3;
EditText editText4;
String test;
int count = 3;
//--------------------------- --------------------------------------------------------
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Makes Button
setContentView(R.layout.activity_main);
declareTextBox();
setButton();
String choice1 = editText.getText().toString();
String choice2 = editText2.getText().toString();
String choice3 = editText3.getText().toString();
choices[0] = choice1; //pass from click button to method.
choices[1] = choice2;
choices[2] = choice3;
finalChoice =rand.nextInt(2);
}
public void setButton()
{
final Button mainbutton = (Button) findViewById(R.id.mainButton);
mainbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
test = ((EditText) findViewById(R.id.editText4)).getText().toString();
// count++;
//retChoice();
// loadScreen();
Intent i = new Intent(MainActivity.this, resultScreen.class);
i.putExtra("display" , displayChoice);
Intent intent = new Intent(MainActivity.this,loadingScreen.class);
//start the second Activity
MainActivity.this.startActivity(intent);
}
});
}
public void declareTextBox()
{
editText = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
}
public void getString(String finalChoice)
{
finalChoice = displayChoice;
}
public String retChoice()
{
displayChoice = choices[finalChoice];
return displayChoice;
}
public void clearText()
{
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setText(" ");
}
});
editText2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText2.setText(" ");
}
});
editText3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText3.setText(" ");
}
});
}
public void getText2()
{
}
public void getText()
{
}
#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;
}
}
This is my Display class :
public class resultScreen extends Activity {
MainActivity ma = new MainActivity();
//Method supposedly retrieves the string data from MainActivity Class but somehow displayed null instead.
//Find a way to keep the string variable when transfering from one class to another class.
String finalResult = ma.retChoice();
public void onCreate(Bundle resultScreen){
super.onCreate(resultScreen);
setContentView(R.layout.resultscreen);
//ma.displayChoice.toString();
String str = finalResult;
TextView text = (TextView) findViewById(R.id.textView1);
text.setText(str);
final Button backbutton = (Button) findViewById(R.id.backButton);
backbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(resultScreen.this,MainActivity.class);
resultScreen.this.startActivity(intent);
}
});
}
}
And this is my loading screen class right after the main button is clicked
public class loadingScreen extends Activity{
protected void onCreate(Bundle loadingScreen) {
// TODO Auto-generated method stub
super.onCreate(loadingScreen);
setContentView(R.layout.loadingscreen);
//If sound clip 20 sec long we don't want to carryit outside next class
// A timer thread looking for "run" method
Thread timer = new Thread()
{
public void run() {
try {
//this is how many mil sec
sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = new Intent(loadingScreen.this, resultScreen.class);
loadingScreen.this.startActivity(intent);
loadingScreen.this.finish();
}
}
};
timer.start();
}
}
My app crashes a few seconds into the loading screen when the program attempts to display the data on the resultScreen.
Here's my logCat
05-17 22:32:54.446: D/AndroidRuntime(1181): Shutting down VM
05-17 22:32:54.446: W/dalvikvm(1181): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
05-17 22:32:54.636: E/AndroidRuntime(1181): FATAL EXCEPTION: main
05-17 22:32:54.636: E/AndroidRuntime(1181): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.choiceprototest/com.example.choiceprototest.resultScreen}: java.lang.NullPointerException
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.os.Looper.loop(Looper.java:137)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.main(ActivityThread.java:5039)
05-17 22:32:54.636: E/AndroidRuntime(1181): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 22:32:54.636: E/AndroidRuntime(1181): at java.lang.reflect.Method.invoke(Method.java:511)
05-17 22:32:54.636: E/AndroidRuntime(1181): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-17 22:32:54.636: E/AndroidRuntime(1181): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-17 22:32:54.636: E/AndroidRuntime(1181): at dalvik.system.NativeStart.main(Native Method)
05-17 22:32:54.636: E/AndroidRuntime(1181): Caused by: java.lang.NullPointerException
05-17 22:32:54.636: E/AndroidRuntime(1181): at com.example.choiceprototest.MainActivity.retChoice(MainActivity.java:101)
05-17 22:32:54.636: E/AndroidRuntime(1181): at com.example.choiceprototest.resultScreen.<init>(resultScreen.java:18)
05-17 22:32:54.636: E/AndroidRuntime(1181): at java.lang.Class.newInstanceImpl(Native Method)
05-17 22:32:54.636: E/AndroidRuntime(1181): at java.lang.Class.newInstance(Class.java:1319)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-17 22:32:54.636: E/AndroidRuntime(1181): ... 11 more
05-17 22:33:03.385: I/Process(1181): Sending signal. PID: 1181 SIG: 9
05-17 22:33:05.435: E/Trace(1204): error opening trace file: No such file or directory (2)
Thanks guys.
I believe I see the problem. You are trying to access your Array but it is an instance variable instead of static so it is being killed off when you exit your Activity. You can either put them in a static class and access them that way or pass a String variable through your Intent, depending on what you need.
If you have the value of the String you need when you leave MainActivity then I would probably just pass it through your Intent. If the value depended on something in one of your other Activities then I would consider putting it in a separate class but it doesn't look like that's what you need here
Edit
You are already doing it here, kind of
Intent i = new Intent(MainActivity.this, resultScreen.class);
i.putExtra("display" , displayChoice);
Intent intent = new Intent(MainActivity.this,loadingScreen.class);
But you aren't starting the Activity that would get the String
i.putExtra("display" , displayChoice);
that line would pass a String with key "displpay" and value of displayChoice, although I think that value is an empty String at that point, but you don't start that Activity. I'm lost on how your flow is suppose to work but basically you would do something like this
String displayChoice = "testValue"; // some arbitrary value--whatever you need to send
Intent i = new Intent(MainActivity.this, resultScreen.class); // create the Intent
i.putExtra("display" , displayChoice); // add the extra
startActivity(i); //start the Activity
then to get the value inside resultScreen in onCreate()
Intent recIntent = getIntent(); // get the Intent that started this Activity
String value = recIntent.getStringExtra("display"); // get the value using the key
// value now equals "testValue"
I think your app chash because you have an ANR: "Application Not Responding" because you are running a long procces inside the UIThread. (onCreate() method is form the UIThread)
Use rather as an Asyntack for your sleeping thread or one handler (with messages).
If you need more I can edit your code tomorrow.