I'm not getting a direct error in Eclipse.. but when trying to run this on my phone it doesn't open the activity and then my phone resets. I might be using the array adapter wrong.. but here's my code files:
Java File
package creativecoders.periodictable;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AM extends Activity {
private ListView amList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.am);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
#Override
public void onStart() {
super.onStart();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.AM, android.R.layout.simple_list_item_1);
amList.setAdapter(adapter);
}
public void onPause() {
super.onPause();
finish();
}
public void onStop() {
super.onStop();
finish();
}
public void onDestroy() {
super.onDestroy();
finish();
}
}
XML Layout File
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
String File
<string-array name="AM">
<item> ONE </item>
<item> TWO </item>
<item> THREE </item>
</string-array>
EDIT: Log file:
04-20 23:10:07.660: D/AndroidRuntime(284): Shutting down VM
04-20 23:10:07.660: W/dalvikvm(284): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-20 23:10:07.670: E/AndroidRuntime(284): FATAL EXCEPTION: main
04-20 23:10:07.670: E/AndroidRuntime(284): java.lang.RuntimeException: Unable to start activity ComponentInfo{creativecoders.periodictable/creativecoders.periodictable.AM}: java.lang.NullPointerException
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.os.Looper.loop(Looper.java:123)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-20 23:10:07.670: E/AndroidRuntime(284): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 23:10:07.670: E/AndroidRuntime(284): at java.lang.reflect.Method.invoke(Method.java:521)
04-20 23:10:07.670: E/AndroidRuntime(284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-20 23:10:07.670: E/AndroidRuntime(284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-20 23:10:07.670: E/AndroidRuntime(284): at dalvik.system.NativeStart.main(Native Method)
04-20 23:10:07.670: E/AndroidRuntime(284): Caused by: java.lang.NullPointerException
04-20 23:10:07.670: E/AndroidRuntime(284): at creativecoders.periodictable.AM.onStart(AM.java:28)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.Activity.performStart(Activity.java:3781)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
04-20 23:10:07.670: E/AndroidRuntime(284): ... 11 more
you did not initialize the ListView. in onCreate()
amList = (ListView)findViewById(R.id.listView1);
Related
firstly i am sorry for my broken english. I wanna read txt file and into 2d array. this code is succesfully running on java. but android emulator gives warning message: "Sorry! the application has stopped unexpectedly. please try again"
how can I fix that? thanks
here is my code:
package com.example.hocam;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Konular extends ActionBarActivity {
private ListView konuListe;
private List<KonuBilgileri> konuBilgileri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_konular);
Bundle dersadi = getIntent().getExtras();
String dersinadi= dersadi.getString("Ders");
switch(dersinadi) {
case "Turkce":
KonuGetir turkce=new KonuGetir("turkce.txt");
String[][] turkcekonular=turkce.getKonular();
System.out.println(turkcekonular[0][0]); // it is the problem and give error
konuListe = (ListView) findViewById(R.id.konu_listesi);
konuBilgileri = new ArrayList<KonuBilgileri>();
konuBilgileri.add(new KonuBilgileri(turkcekonular[0][0],R.drawable.turkce, turkcekonular[0][1])); // array is problem
MyAdapter adapter = new MyAdapter(Konular.this, R.layout.custom_list_item, konuBilgileri);
konuListe.setAdapter(adapter);
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.konular, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class KonuGetir {
final int maxLines = 10100;
String[][] resultArray = new String[maxLines][];
public KonuGetir(String Ders){
File file = new File(Ders);
Scanner scanner;
try {
scanner = new Scanner(file,"utf-8");
int linesCounter = 0;
while (scanner.hasNextLine() && linesCounter < maxLines) {
resultArray[linesCounter] = scanner.nextLine().split("#");
linesCounter++;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String[][] getKonular() {
return resultArray;
}
}
logfile:
> 09-04 16:18:22.262: E/AndroidRuntime(1164): FATAL EXCEPTION: main
09-04 16:18:22.262: E/AndroidRuntime(1164): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hocam/com.example.hocam.Konular}: java.lang.NullPointerException
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.os.Looper.loop(Looper.java:123)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 16:18:22.262: E/AndroidRuntime(1164): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:18:22.262: E/AndroidRuntime(1164): at java.lang.reflect.Method.invoke(Method.java:521)
09-04 16:18:22.262: E/AndroidRuntime(1164): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-04 16:18:22.262: E/AndroidRuntime(1164): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-04 16:18:22.262: E/AndroidRuntime(1164): at dalvik.system.NativeStart.main(Native Method)
09-04 16:18:22.262: E/AndroidRuntime(1164): Caused by: java.lang.NullPointerException
09-04 16:18:22.262: E/AndroidRuntime(1164): at com.example.hocam.Konular.onCreate(Konular.java:66)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-04 16:18:22.262: E/AndroidRuntime(1164): ... 11 more
09-04 16:42:10.422: E/AndroidRuntime(1178): FATAL EXCEPTION: main
09-04 16:42:10.422: E/AndroidRuntime(1178): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hocam/com.example.hocam.Konular}: java.lang.NullPointerException
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.os.Looper.loop(Looper.java:123)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 16:42:10.422: E/AndroidRuntime(1178): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:42:10.422: E/AndroidRuntime(1178): at java.lang.reflect.Method.invoke(Method.java:521)
09-04 16:42:10.422: E/AndroidRuntime(1178): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-04 16:42:10.422: E/AndroidRuntime(1178): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-04 16:42:10.422: E/AndroidRuntime(1178): at dalvik.system.NativeStart.main(Native Method)
09-04 16:42:10.422: E/AndroidRuntime(1178): Caused by: java.lang.NullPointerException
09-04 16:42:10.422: E/AndroidRuntime(1178): at com.example.hocam.Konular.onCreate(Konular.java:66)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-04 16:42:10.422: E/AndroidRuntime(1178): ... 11 more
09-04 16:43:28.802: E/AndroidRuntime(1207): FATAL EXCEPTION: main
09-04 16:43:28.802: E/AndroidRuntime(1207): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hocam/com.example.hocam.Konular}: java.lang.NullPointerException
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.os.Looper.loop(Looper.java:123)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 16:43:28.802: E/AndroidRuntime(1207): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:43:28.802: E/AndroidRuntime(1207): at java.lang.reflect.Method.invoke(Method.java:521)
09-04 16:43:28.802: E/AndroidRuntime(1207): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-04 16:43:28.802: E/AndroidRuntime(1207): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-04 16:43:28.802: E/AndroidRuntime(1207): at dalvik.system.NativeStart.main(Native Method)
09-04 16:43:28.802: E/AndroidRuntime(1207): Caused by: java.lang.NullPointerException
09-04 16:43:28.802: E/AndroidRuntime(1207): at com.example.hocam.Konular.onCreate(Konular.java:66)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-04 16:43:28.802: E/AndroidRuntime(1207): ... 11 more
android manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".YGS"
android:label="#string/title_activity_ygs" >
</activity>
<activity
android:name=".Konular"
android:label="#string/title_activity_konular" >
</activity>
<activity
android:name=".Video"
android:screenOrientation="landscape"
android:label="#string/title_activity_video" >
</activity>
</application>
You are using the following code:
String[][] turkcekonular=turkce.getKonular();
System.out.println(turkcekonular[0][0]);
But get java.lang.NullPointerException.
The reason for this because getKonular() returns an array initalised to size 10100, however scanner does not contain any lines so scanner.hasNextLine() is false.
This means that turkcekonular[0] is null, as it was never set.
Which means that turkcekonular[0][0] is trying to access a null object, so throws a NullPointerException
You should check if the object is null before trying to access it..
if(turkcekonular[0] != null)
System.out.println(turkcekonular[0][0]);
and else where that this issue occurs.
(and you should figure out why your file is not read. Is it named correctly and exists in the specified path?)
I am writing an Android application in Eclipse that uses RitaWordNet to find synonyms for a word. It compiles but on running it crashes. Is it possible that the app crashes and I receive the message "Unfortunately RitaWord has stopped working". Is it because the jar files I have added to the build path to make the app work are so large (>10 MB)? The program is:
package com.example.ritaword;
import java.util.Arrays;
import android.*
import rita.wordnet.*;
public class MainActivity extends Activity {
TextView A, B;
Button ok;
String[] synonyms;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ok = (Button)findViewById(android.R.id.button1);
A = (TextView)findViewById(R.id.textView1);
B = (TextView)findViewById(R.id.textView2);
B.setText("");
RiWordnet wordnet = new RiWordnet();
String word = wordnet.getRandomWord("a");
synonyms = wordnet.getAllSynonyms(word, "a", 1);
}
public void onClick(View view) {
switch (view.getId()) {
case (R.id.button1):
if (synonyms != null) {
Arrays.sort(synonyms);
B.setText(synonyms[0]);
}
else
B.setText("No synyonyms!");
}
}
#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;
}
}
If it helps, the last few error Logs I receive are:
>04-20 22:25:08.239: E/ActivityThread(730): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
04-20 22:25:08.239: E/ActivityThread(730): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
04-20 22:25:08.239: E/ActivityThread(730): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
04-20 22:25:08.239: E/ActivityThread(730): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
04-20 22:25:08.239: E/ActivityThread(730): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-20 22:25:08.239: E/ActivityThread(730): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-20 22:25:08.239: E/ActivityThread(730): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-20 22:25:08.239: E/ActivityThread(730): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-20 22:25:08.239: E/ActivityThread(730): at java.lang.Thread.run(Thread.java:856)
04-20 22:25:08.319: E/StrictMode(730): null
04-20 22:25:08.319: E/StrictMode(730): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection#40cea428 that was originally bound here
04-20 22:25:08.319: E/StrictMode(730): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
04-20 22:25:08.319: E/StrictMode(730): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
04-20 22:25:08.319: E/StrictMode(730): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
04-20 22:25:08.319: E/StrictMode(730): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
04-20 22:25:08.319: E/StrictMode(730): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
04-20 22:25:08.319: E/StrictMode(730): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
04-20 22:25:08.319: E/StrictMode(730): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
04-20 22:25:08.319: E/StrictMode(730): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-20 22:25:08.319: E/StrictMode(730): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-20 22:25:08.319: E/StrictMode(730): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-20 22:25:08.319: E/StrictMode(730): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-20 22:25:08.319: E/StrictMode(730): at java.lang.Thread.run(Thread.java:856)
i have made 2 classes YehActivity.java and h.java. On running the application i am getting an error ,Application has stopped unexpectedly.Here is the code
public class YehActivity extends Activity {
public static final int r=1;
Button b;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==r && resultCode==RESULT_OK){
String h=data.getStringExtra("a");
tv.setText(h);
}
}
}
where to check for null.
this is the second file
public class he extends Activity{
Button b;
EditText et;
Intent i=getIntent();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.h);
b=(Button) findViewById(R.id.button12);
et=(EditText) findViewById(R.id.editText1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String value=et.getText().toString().trim();
i.putExtra("a", value);
he.this.setResult(RESULT_OK, i);
finish();
}
});
}
}
and the log file is
02-11 23:31:46.408: I/Process(302): Sending signal. PID: 302 SIG: 9
02-11 23:45:04.778: D/AndroidRuntime(357): Shutting down VM
02-11 23:45:04.778: W/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:04.798: E/AndroidRuntime(357): FATAL EXCEPTION: main
02-11 23:45:04.798: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:04.798: E/AndroidRuntime(357): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): Caused by: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:04.798: E/AndroidRuntime(357): ... 11 more
02-11 23:45:11.158: I/Process(357): Sending signal. PID: 357 SIG: 9
02-11 23:45:22.708: D/AndroidRuntime(374): Shutting down VM
02-11 23:45:22.708: W/dalvikvm(374): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:22.728: E/AndroidRuntime(374): FATAL EXCEPTION: main
02-11 23:45:22.728: E/AndroidRuntime(374): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:22.728: E/AndroidRuntime(374): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): Caused by: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:22.728: E/AndroidRuntime(374): ... 11 more
02-11 23:45:25.497: I/Process(374): Sending signal. PID: 374 SIG: 9
In your
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
onCreate() method you attempt to use b, but you never initialize it (I'm assuming its declared as a global variable). This means that you will run into a NullPointerException when you try to call setOnClickListener().
In your code in OnCreate() you have to declare b as button and then apply listener to that.
b=(Button) findViewById(R.id.button12);
Also check your data is null or not. If it is null than handle it properly.
Then your code runs fine.
I'm trying to test if my android app works, it consists of 2 activity screens. There are no errors in my codes but my app won't run. It always gives me this error on the emulator "Unfortunately 'application name' has stopped."
Here is my Activity code
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View title = getWindow().findViewById(android.R.id.title);
View titleBar = (View) title.getParent();
titleBar.setBackgroundColor(Color.RED);
Button next=(Button)findViewById(R.id.DGButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent=new Intent(view.getContext(),
Activity2.class);
startActivityForResult(myIntent, 0);
}});}
public void calculateClickHandler(View view)
{
if (view.getId() == R.id.CalculateButton)
{
EditText ageText = (EditText)findViewById
(R.id.AgeField);
EditText weightText = (EditText)findViewById
(R.id.WeightField);
EditText ftText = (EditText)findViewById
(R.id.HeightField);
EditText inText = (EditText)findViewById
(R.id.HeightField2);
RadioGroup weightRG = (RadioGroup) findViewById
(R.id.WeightRG);
RadioGroup sexRG = (RadioGroup) findViewById
(R.id.SexRG);
TextView resultText = (TextView)findViewById
(R.id.ResultLabel);
TextView normalBMIText = (TextView)findViewById
(R.id.NormalBMI);
TextView idealKgText = (TextView)findViewById
(R.id.IdealKgLabel);
TextView idealLbText = (TextView)findViewById
(R.id.IdealLbLabel);
int age = Integer.parseInt(ageText.getText
().toString());
double weight = Double.parseDouble
(weightText.getText().toString());
double ftheight = Double.parseDouble(ftText.getText
().toString());
double inheight = Double.parseDouble(inText.getText
().toString());
int checkedRadioButton1 =
weightRG.getCheckedRadioButtonId();
int checkedRadioButton2 =
sexRG.getCheckedRadioButtonId();
double bmiValue = calculateBMI(weight, ftheight,
inheight, checkedRadioButton1);
String bmiInterpretation1 = interpretBMI1(bmiValue);
String bmiInterpretation2 = interpretBMI2(age);
String bmiInterpretation3 = interpretBMI3(ftheight,
inheight, checkedRadioButton2);
String bmiInterpretation4 = interpretBMI4(ftheight,
inheight, checkedRadioButton2);
resultText.setText(bmiValue + " - " +
bmiInterpretation1);
normalBMIText.setText(""+bmiInterpretation2);
idealKgText.setText(""+bmiInterpretation3);
idealLbText.setText(""+bmiInterpretation4);
Intent intent1 = new Intent(MainActivity.this,
Activity2.class);
Bundle b = new Bundle();
b.putDouble("key", bmiValue);
intent1.putExtras(b);
startActivity(intent1);
}}
This is the error logcat:
12-26 02:50:45.606: E/AndroidRuntime(1776): FATAL EXCEPTION: main
12-26 02:50:45.606: E/AndroidRuntime(1776): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bmicaldg/com.example.bmicaldg.MainActivity}: java.lang.NullPointerException
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.os.Looper.loop(Looper.java:137)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-26 02:50:45.606: E/AndroidRuntime(1776): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 02:50:45.606: E/AndroidRuntime(1776): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 02:50:45.606: E/AndroidRuntime(1776): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-26 02:50:45.606: E/AndroidRuntime(1776): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-26 02:50:45.606: E/AndroidRuntime(1776): at dalvik.system.NativeStart.main(Native Method)
12-26 02:50:45.606: E/AndroidRuntime(1776): Caused by: java.lang.NullPointerException
12-26 02:50:45.606: E/AndroidRuntime(1776): at com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.Activity.performCreate(Activity.java:5104)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-26 02:50:45.606: E/AndroidRuntime(1776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-26 02:50:45.606: E/AndroidRuntime(1776): ... 11 more
This is the debug logcat:
12-26 03:04:06.238: I/Process(1854): Sending signal. PID: 1854 SIG: 9
12-26 03:04:12.779: W/Trace(1876): Unexpected value from nativeGetEnabledTags: 0
12-26 03:04:12.837: W/Trace(1876): Unexpected value from nativeGetEnabledTags: 0
12-26 03:04:14.866: D/dalvikvm(1876): GC_CONCURRENT freed 76K, 7% free 2723K/2916K, paused 32ms+32ms, total 270ms
12-26 03:04:15.427: D/AndroidRuntime(1876): Shutting down VM
12-26 03:04:15.456: W/dalvikvm(1876): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-26 03:04:15.546: E/AndroidRuntime(1876): FATAL EXCEPTION: main
12-26 03:04:15.546: E/AndroidRuntime(1876): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bmicaldg/com.example.bmicaldg.MainActivity}: java.lang.NullPointerException
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.os.Looper.loop(Looper.java:137)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-26 03:04:15.546: E/AndroidRuntime(1876): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 03:04:15.546: E/AndroidRuntime(1876): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 03:04:15.546: E/AndroidRuntime(1876): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-26 03:04:15.546: E/AndroidRuntime(1876): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-26 03:04:15.546: E/AndroidRuntime(1876): at dalvik.system.NativeStart.main(Native Method)
12-26 03:04:15.546: E/AndroidRuntime(1876): Caused by: java.lang.NullPointerException
12-26 03:04:15.546: E/AndroidRuntime(1876): at com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.Activity.performCreate(Activity.java:5104)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-26 03:04:15.546: E/AndroidRuntime(1876): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-26 03:04:15.546: E/AndroidRuntime(1876): ... 11 more
12-26 03:04:22.547: I/Process(1876): Sending signal. PID: 1876 SIG: 9
Any help is appreciated
02:50:45.606: E/AndroidRuntime(1776):
Caused by: java.lang.NullPointerException 12-26 02:50:45.606: E/AndroidRuntime(1776): at
com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25) 12-26
There is no code in your question so hard to tell how to fix, but based on stack trace, line 25 in MainActivity.java throwing NullPointerException. Code at line25 is some how resulting as null and you are trying to call some action on null reference which results in NullPointerException.
This is caused by NullPointerException. You might be accesing the null object which hasn't been initialized yet. You edit your question with your code, so that there would be chances of having my answer edited.
Caused by: java.lang.NullPointerException 12-26 02:50:45.606: E/AndroidRuntime(1776): at com.example.bmicaldg.MainActivity.onCreate(MainActivity.java:25)
This is where your error lies, line 25 of your MainActivity class. Whatever you are referencing is null.
Change your MainActivity onCreate code as:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View title = getWindow().findViewById(android.R.id.title);
if (title != null) {
ViewParent titleBar = title.getParent();
if (titleBar != null && (titleBar instanceof View)) {
View parentView = (View)titleBar;
parentView.setBackgroundColor(Color.RED);
}
}
// Your Code here...
package com.markana.yamba;
import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.TwitterException;
import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
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 StatusActivity2 extends Activity implements OnClickListener{
private static final String TAG= "StatusActivity";
EditText editText;
Button updateButton;
TextView textCount;
Twitter twitter;
/** Called when the activity is first created. */
#SuppressWarnings("deprecation")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
//Finds views
editText =(EditText) findViewById(R.id.editText);
updateButton=(Button) findViewById(R.id.buttonUpdate);
updateButton.setOnClickListener(this);
twitter=new Twitter("student","password");
twitter.setAPIRootUrl(("http://yamba.marakana.com/api"));
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
String status=editText.getText().toString();
new PostToTwitter().execute(status);
Log.d(TAG, "onClick");
}
class PostToTwitter extends AsyncTask<String,Integer, String>
{
#Override
protected String doInBackground(String... statuses) {
try{
Twitter.Status status=twitter.updateStatus(statuses[0]);
return status.text;
}
catch(TwitterException e)
{
Log.e(TAG,e.toString());
e.printStackTrace();
return "Failed to post";
}
}
protected void onProgressUpdate(Integer...values)
{
super.onProgressUpdate(values);
}
protected void onPostExecute(String result)
{
Toast.makeText(StatusActivity2.this,result,Toast.LENGTH_LONG).show();
}
}
}
i get two exceptions relating to Async task
thats what i did. it tells me that i have a problem when i run the background worker!!
04-20 09:21:32.553: ERROR/AndroidRuntime(838): FATAL EXCEPTION: AsyncTask #1
04-20 09:21:32.553: ERROR/AndroidRuntime(838): java.lang.RuntimeException: An error occured while executing doInBackground()
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.lang.Thread.run(Thread.java:1096)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): Caused by: java.lang.IllegalArgumentException
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.Date.parse(Date.java:447)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.Date.<init>(Date.java:157)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter$Status.<init>(Twitter.java:659)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter.updateStatus(Twitter.java:3231)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter.updateStatus(Twitter.java:3161)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at com.markana.yamba.StatusActivity2$PostToTwitter.doInBackground(StatusActivity2.java:60)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at com.markana.yamba.StatusActivity2$PostToTwitter.doInBackground(StatusActivity2.java:1)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): ... 4 more
it should pop a message in the end.. as you can see in the method onClick!
Twitter.java:659
are you using some date creation here ? looks like you are parsing a string to date object and string is not in the date format specified.
below is ur problem part. nothing to do with task creation i guess.what format is 04-20 09:21:32.553
i guess it should be somewat like mm-dd-yy hh:mm:ss.nnn or some other standard format
Caused by: java.lang.IllegalArgumentException
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.Date.parse(Date.java:447)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.Date.<init>(Date.java:157)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter$Status.<init>(Twitter.java:659)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter.updateStatus(Twitter.java:3231)
It may be that the
Toast.makeText(StatusActivity2.this,res..
fails, as the context you pass in is not valid.
Try to pass in the Context in the Constructor of PostToTwitter, store it locally and then use this instance when creating the Toast.
See e.g. here for the call, here for the Constructor and here for the onPostExecute
Assuming I understand properly: update your screen in the onPostExecute method. You might want to have a member variable of your activity holding the current task. The in your onClick function, simply check if a task is already running before creating a new one.
Also, don't forget to remove the click listener when you destroy your activity, else you will leak memory.
protected void onDestroy() {
updateButton.setOnClickListener(null);
super.onDestroy();
}
I don't think it has anything to do with AsyncTask or Context. The below line is failing which is doing some date parsing. Can you try calling below in a main thread without AsyncTask and see if it is working?.
Twitter.Status status=twitter.updateStatus(statuses[0]);