我是一名新的Android程序员,最近,我的很多项目都收到了这个错误:
07-31 23:45:19.592: ERROR/AndroidRuntime(716): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.amrit.musifind/com.amrit.musifind.Main}:
java.lang.NullPointerException
有人可以帮我解决这个问题吗?
这是整个logcat堆栈跟踪:
07-31 23:45:18.512: INFO/ActivityManager(63): Starting activity: Intent {
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000
cmp=com.amrit.musifind/.Main }
07-31 23:45:18.682: INFO/ActivityManager(63): Start proc com.amrit.musifind for activity
com.amrit.musifind/.Main: pid=716 uid=10025 gids={3003}
07-31 23:45:18.962: INFO/jdwp(716): received file descriptor 20 from ADB
07-31 23:45:19.102: DEBUG/ddm-heap(716): Got feature list request
07-31 23:45:19.572: DEBUG/AndroidRuntime(716): Shutting down VM
07-31 23:45:19.572: WARN/dalvikvm(716): threadid=3: thread exiting with uncaught
exception (group=0x4001aa28)
07-31 23:45:19.572: ERROR/AndroidRuntime(716): Uncaught handler: thread main exiting due
to uncaught exception
07-31 23:45:19.592: ERROR/AndroidRuntime(716): java.lang.RuntimeException: Unable to
start activity ComponentInfo{com.amrit.musifind/com.amrit.musifind.Main}:
java.lang.NullPointerException
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
android.app.ActivityThread.access$2100(ActivityThread.java:116)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
android.os.Handler.dispatchMessage(Handler.java:99)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
android.os.Looper.loop(Looper.java:123)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
android.app.ActivityThread.main(ActivityThread.java:4203)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
java.lang.reflect.Method.invokeNative(Native Method)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
java.lang.reflect.Method.invoke(Method.java:521)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
dalvik.system.NativeStart.main(Native Method)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): Caused by: java.lang.NullPointerException
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
com.amrit.musifind.Main.onCreate(Main.java:44)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): ... 11 more
07-31 23:45:19.612: INFO/Process(63): Sending signal. PID: 716 SIG: 3
07-31 23:45:19.612: INFO/dalvikvm(716): threadid=7: reacting to signal 3
07-31 23:45:19.642: INFO/dalvikvm(716): Wrote stack trace to '/data/anr/traces.txt'
07-31 23:45:21.422: INFO/Process(716): Sending signal. PID: 716 SIG: 9
07-31 23:45:21.442: INFO/ActivityManager(63): Process com.amrit.musifind (pid 716) has
died.
07-31 23:45:21.583: WARN/UsageStats(63): Unexpected resume of com.android.launcher while
already resumed in com.amrit.musifind
07-31 23:45:21.712: WARN/InputManagerService(63): Window already focused, ignoring focus
gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@439559d0
07-31 23:45:23.482: DEBUG/AndroidRuntime(725): >>>>>>>>>>>>>> AndroidRuntime START
<<<<<<<<<<<<<<
07-31 23:45:23.482: DEBUG/AndroidRuntime(725): CheckJNI is ON
07-31 23:45:23.812: DEBUG/AndroidRuntime(725): --- registering native functions ---
07-31 23:45:23.832: INFO/jdwp(725): received file descriptor 20 from ADB
07-31 23:45:24.193: DEBUG/ddm-heap(725): Got feature list request
07-31 23:45:25.052: DEBUG/AndroidRuntime(725): Shutting down VM
07-31 23:45:25.052: DEBUG/dalvikvm(725): DestroyJavaVM waiting for non-daemon threads to
exit
07-31 23:45:25.062: DEBUG/dalvikvm(725): DestroyJavaVM shutting VM down
07-31 23:45:25.062: DEBUG/dalvikvm(725): HeapWorker thread shutting down
07-31 23:45:25.062: DEBUG/dalvikvm(725): HeapWorker thread has shut down
07-31 23:45:25.073: DEBUG/jdwp(725): JDWP shutting down net...
07-31 23:45:25.073: DEBUG/jdwp(725): +++ peer disconnected
07-31 23:45:25.073: INFO/dalvikvm(725): Debugger has detached; object registry had 1
entries
07-31 23:45:25.093: DEBUG/dalvikvm(725): VM cleaning up
07-31 23:45:25.113: DEBUG/dalvikvm(725): LinearAlloc 0x0 used 665652 of 4194304 (15%)
07-31 23:47:22.712: DEBUG/dalvikvm(104): GC freed 9173 objects / 524184 bytes in 149ms
继承第一项活动:start.java
package com.amrit.musifind;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import com.amrit.musifind.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public abstract class start extends Activity implements OnClickListener {
Button buttonGo, buttonReset;
EditText EditTextinput;
String input = "";
String ur = "http://www.tastekid.com/ask/ws?q=";
String l = "&f=musifin2125&k=mjjlnzkyzwuz&format=JSON";
String url = "" ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonGo = (Button) findViewById(R.id.buttonGo);
buttonReset = (Button) findViewById(R.id.buttonReset);
EditTextinput = (EditText) findViewById(R.id.EditTextinput);
//Button listener
buttonReset.setOnClickListener(this);
buttonGo.setOnClickListener(this);
}
public void onClick (View src){
switch(src.getId()){
case R.id.buttonGo:
input = EditTextinput.getText().toString();
url = ur + input + l ;
Intent Main = new Intent(this,
ToClass.class);
Main.putExtra("userurl", url);
startActivity(Main);
break;
case R.id.buttonReset:
EditTextinput.setText("");
break;
}
}
}
第二项活动:Main.java
package com.amrit.musifind;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.amrit.musifind.JSONfunctions;
import com.amrit.musifind.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
Intent intent = getIntent();
String url = intent.getExtras().getString("userurl");
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>
();
JSONObject json = JSONfunctions.getJSONfromURL(url);
try{
JSONObject earthquakes = json.getJSONObject("Similar");
JSONArray info = earthquakes.getJSONArray("Results");
for (int i = 0; i < info.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = info.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "Name:" + e.getString("Name"));
map.put("type", "Type: " + e.getString("Type"));
mylist.add(map);
}
JSONArray results = json.getJSONArray("Results");
for(int i=0;i<results.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = results.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "Name:" + e.getString("name"));
map.put("type", "Type: " + e.getString("type"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "name", "type" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long
id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>)
lv.getItemAtPosition(position);
Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.",
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.amrit.musifind"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".start"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.start" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name= ".Main"
>
<intent-filter>
<action android:name="android.intent.action.Main" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
非常感谢任何帮助,如果没有解决这个问题,我无法测试我的应用程序!
答案 0 :(得分:27)
您的空指针异常似乎在这一行:
String url = intent.getExtras().getString("userurl");
因为intent.getExtras()
在intent没有任何额外内容时返回null。
你必须意识到这段代码:
Intent Main = new Intent(this, ToClass.class);
Main.putExtra("userurl", url);
startActivity(Main);
不会启动您在Main.java中编写的活动,它会尝试启动名为ToClass
的活动,如果该活动不存在,您的应用会崩溃。
此外,没有"android.intent.action.start"
这样的东西,所以清单看起来应该更像:
<activity android:name=".start" 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= ".Main">
</activity>
我希望这可以解决您遇到的一些问题,但我强烈建议您查看一些“入门”教程以进行Android开发并从那里进行构建。
答案 1 :(得分:1)
问题已经得到解答,但是我想添加更多有关原因的信息。
Android应用无法启动活动componentinfo
该错误通常与相应的日志一起出现。您可以阅读日志并轻松解决此问题。
这是示例日志。您可以在其中清楚地看到ClassCastException
。之所以出现此问题是因为无法将TextView
强制转换为EditText
。
原因:java.lang.ClassCastException:android.widget.TextView 无法转换为android.widget.EditText
11-04 01:24:10.403: D/AndroidRuntime(1050): Shutting down VM
11-04 01:24:10.403: W/dalvikvm(1050): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-04 01:24:10.543: E/AndroidRuntime(1050): FATAL EXCEPTION: main
11-04 01:24:10.543: E/AndroidRuntime(1050): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.troysantry.tipcalculator/com.troysantry.tipcalculator.TipCalc}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-04 01:24:10.543: E/AndroidRuntime(1050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-04 01:24:10.543: E/AndroidRuntime(1050): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-04 01:24:10.543: E/AndroidRuntime(1050): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-04 01:24:10.543: E/AndroidRuntime(1050): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-04 01:24:10.543: E/AndroidRuntime(1050): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 01:24:10.543: E/AndroidRuntime(1050): at android.os.Looper.loop(Looper.java:137)
11-04 01:24:10.543: E/AndroidRuntime(1050): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-04 01:24:10.543: E/AndroidRuntime(1050): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 01:24:10.543: E/AndroidRuntime(1050): at java.lang.reflect.Method.invoke(Method.java:525)
11-04 01:24:10.543: E/AndroidRuntime(1050): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-04 01:24:10.543: E/AndroidRuntime(1050): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-04 01:24:10.543: E/AndroidRuntime(1050): at dalvik.system.NativeStart.main(Native Method)
11-04 01:24:10.543: E/AndroidRuntime(1050): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-04 01:24:10.543: E/AndroidRuntime(1050): at com.troysantry.tipcalculator.TipCalc.onCreate(TipCalc.java:45)
11-04 01:24:10.543: E/AndroidRuntime(1050): at android.app.Activity.performCreate(Activity.java:5133)
11-04 01:24:10.543: E/AndroidRuntime(1050): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-04 01:24:10.543: E/AndroidRuntime(1050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-04 01:24:10.543: E/AndroidRuntime(1050): ... 11 more
11-04 01:29:11.177: I/Process(1050): Sending signal. PID: 1050 SIG: 9
11-04 01:31:32.080: D/AndroidRuntime(1109): Shutting down VM
11-04 01:31:32.080: W/dalvikvm(1109): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-04 01:31:32.194: E/AndroidRuntime(1109): FATAL EXCEPTION: main
11-04 01:31:32.194: E/AndroidRuntime(1109): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.troysantry.tipcalculator/com.troysantry.tipcalculator.TipCalc}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-04 01:31:32.194: E/AndroidRuntime(1109): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-04 01:31:32.194: E/AndroidRuntime(1109): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-04 01:31:32.194: E/AndroidRuntime(1109): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-04 01:31:32.194: E/AndroidRuntime(1109): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-04 01:31:32.194: E/AndroidRuntime(1109): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 01:31:32.194: E/AndroidRuntime(1109): at android.os.Looper.loop(Looper.java:137)
11-04 01:31:32.194: E/AndroidRuntime(1109): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-04 01:31:32.194: E/AndroidRuntime(1109): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 01:31:32.194: E/AndroidRuntime(1109): at java.lang.reflect.Method.invoke(Method.java:525)
11-04 01:31:32.194: E/AndroidRuntime(1109): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-04 01:31:32.194: E/AndroidRuntime(1109): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-04 01:31:32.194: E/AndroidRuntime(1109): at dalvik.system.NativeStart.main(Native Method)
11-04 01:31:32.194: E/AndroidRuntime(1109): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-04 01:31:32.194: E/AndroidRuntime(1109): at com.troysantry.tipcalculator.TipCalc.onCreate(TipCalc.java:44)
11-04 01:31:32.194: E/AndroidRuntime(1109): at android.app.Activity.performCreate(Activity.java:5133)
11-04 01:31:32.194: E/AndroidRuntime(1109): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-04 01:31:32.194: E/AndroidRuntime(1109): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-04 01:31:32.194: E/AndroidRuntime(1109): ... 11 more
11-04 01:36:33.195: I/Process(1109): Sending signal. PID: 1109 SIG: 9
11-04 02:11:09.684: D/AndroidRuntime(1167): Shutting down VM
11-04 02:11:09.684: W/dalvikvm(1167): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-04 02:11:09.855: E/AndroidRuntime(1167): FATAL EXCEPTION: main
11-04 02:11:09.855: E/AndroidRuntime(1167): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.troysantry.tipcalculator/com.troysantry.tipcalculator.TipCalc}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-04 02:11:09.855: E/AndroidRuntime(1167): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-04 02:11:09.855: E/AndroidRuntime(1167): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-04 02:11:09.855: E/AndroidRuntime(1167): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-04 02:11:09.855: E/AndroidRuntime(1167): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-04 02:11:09.855: E/AndroidRuntime(1167): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 02:11:09.855: E/AndroidRuntime(1167): at android.os.Looper.loop(Looper.java:137)
11-04 02:11:09.855: E/AndroidRuntime(1167): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-04 02:11:09.855: E/AndroidRuntime(1167): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 02:11:09.855: E/AndroidRuntime(1167): at java.lang.reflect.Method.invoke(Method.java:525)
11-04 02:11:09.855: E/AndroidRuntime(1167): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-04 02:11:09.855: E/AndroidRuntime(1167): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-04 02:11:09.855: E/AndroidRuntime(1167): at dalvik.system.NativeStart.main(Native Method)
11-04 02:11:09.855: E/AndroidRuntime(1167): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-04 02:11:09.855: E/AndroidRuntime(1167): at com.troysantry.tipcalculator.TipCalc.onCreate(TipCalc.java:44)
11-04 02:11:09.855: E/AndroidRuntime(1167): at android.app.Activity.performCreate(Activity.java:5133)
11-04 02:11:09.855: E/AndroidRuntime(1167): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-04 02:11:09.855: E/AndroidRuntime(1167): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-04 02:11:09.855: E/AndroidRuntime(1167): ... 11 more
findViewById()
不存在的视图就像您在布局XML中不存在按钮ID时使用findViewById(R.id.button)
一样。
如果您错误地进行了某些授课,则会收到此错误。就像您将RelativeLayout
投射到LinearLayout
或EditText
投射到TextView
一样。
manifest.xml
如果您没有在manifest.xml
中注册“活动”,则会出现此错误。
findViewById()
,并在顶层进行声明以下代码不正确。这将产生错误。因为您应该在调用findViewById()
之后执行setContentView()
。因为创建视图后就可以在那里。
public class MainActivity extends Activity {
ImageView mainImage = (ImageView) findViewById(R.id.imageViewMain); //incorrect way
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainImage = (ImageView) findViewById(R.id.imageViewMain); //correct way
//...
}
}
abstract
活动类。当您尝试启动一个抽象的活动时,将出现此错误。因此,只需在活动类别名称之前删除 abstract
关键字。
如果您的活动是用Kotlin编写的,但尚未在应用程序中设置kotlin。那么你会得到错误。您可以按照Android Link或Kotlin Link编写的简单步骤进行操作。您也可以检查this answer。
Read about Downcast and Upcast