Android App 无法运行,MainActivity 出现空点异常
Posted
技术标签:
【中文标题】Android App 无法运行,MainActivity 出现空点异常【英文标题】:Android App will not run, null point exception at MainActivity 【发布时间】:2014-07-08 06:25:00 【问题描述】:我创建了一个应用程序,在该应用程序中我使用 ArrayAdapter 将行项的值加载到列表视图中,但尝试在我的虚拟设备上运行该应用程序后,我看到以下错误日志:
我的 MainActivity 代码:遵循错误日志
07-08 02:11:07.077: D/androidRuntime(995): Shutting down VM
07-08 02:11:07.107: W/dalvikvm(995): threadid=1: thread exiting with uncaught exception
(group=0x41465700)
07-08 02:11:07.146: E/AndroidRuntime(995): FATAL EXCEPTION: main
07-08 02:11:07.146: E/AndroidRuntime(995): java.lang.RuntimeException: Unable to
instantiate activity
ComponentInfocom.example.deenbuddy/com.example.deenbuddy.MainActivity:
java.lang.NullPointerException
07-08 02:11:07.146: E/AndroidRuntime(995): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
07-08 02:11:07.146: E/AndroidRuntime(995): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-08 02:11:07.146: E/AndroidRuntime(995): at
android.app.ActivityThread.access$600(ActivityThread.java:141)
07-08 02:11:07.146: E/AndroidRuntime(995): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-08 02:11:07.146: E/AndroidRuntime(995): at
android.os.Handler.dispatchMessage(Handler.java:99)
07-08 02:11:07.146: E/AndroidRuntime(995): at android.os.Looper.loop(Looper.java:137)
07-08 02:11:07.146: E/AndroidRuntime(995): at
android.app.ActivityThread.main(ActivityThread.java:5103)
07-08 02:11:07.146: E/AndroidRuntime(995): at
java.lang.reflect.Method.invokeNative(Native Method)
07-08 02:11:07.146: E/AndroidRuntime(995): at
java.lang.reflect.Method.invoke(Method.java:525)
07-08 02:11:07.146: E/AndroidRuntime(995): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-08 02:11:07.146: E/AndroidRuntime(995): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-08 02:11:07.146: E/AndroidRuntime(995): at dalvik.system.NativeStart.main(Native
Method)
07-08 02:11:07.146: E/AndroidRuntime(995): Caused by: java.lang.NullPointerException
07-08 02:11:07.146: E/AndroidRuntime(995): at
android.content.ContextWrapper.getResources(ContextWrapper.java:89)
07-08 02:11:07.146: E/AndroidRuntime(995): at
android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
07-08 02:11:07.146: E/AndroidRuntime(995): at com.example.deenbuddy.MainActivity.<init>
(MainActivity.java:23)
07-08 02:11:07.146: E/AndroidRuntime(995): at java.lang.Class.newInstanceImpl(Native
Method)
07-08 02:11:07.146: E/AndroidRuntime(995): at
java.lang.Class.newInstance(Class.java:1130)
07-08 02:11:07.146: E/AndroidRuntime(995): at
android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-08 02:11:07.146: E/AndroidRuntime(995): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
07-08 02:11:07.146: E/AndroidRuntime(995): ... 11 more
package com.example.deenbuddy;
import java.util.List;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ImageView;
import com.example.deenbuddy.RowItem;
import com.example.deenbuddy.CListView;
public class MainActivity extends Activity
//Arrays for RowItem values
final String[] uname = new String[] getResources().getString(R.string.social),getResources().getString(R.string.social),getResources().getString(R.string.social);
final String[] ustatus = new String[]getResources().getString(R.string.temp_ustatus0),getResources().getString(R.string.temp_ustat us0),getResources().getString(R.string.temp_ustatus0);
final Integer[] upic = new Integer[]R.drawable.tig0,R.drawable.tig0,R.drawable.tig0;
//ListView and RowItem for ListView
ListView listview;
List<RowItem> rowItem;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//News Buttons
ImageView newsb1 = (ImageView) findViewById(R.id.news1);
ImageView newsb2 = (ImageView) findViewById(R.id.news2);
ImageView newsb3 = (ImageView) findViewById(R.id.news3);
ImageView newsb4 = (ImageView) findViewById(R.id.news4);
//Feature Buttons
ImageView featb1 = (ImageView) findViewById(R.id.feat1);
ImageView featb2 = (ImageView) findViewById(R.id.feat2);
ImageView featb3 = (ImageView) findViewById(R.id.feat3);
ImageView featb4 = (ImageView) findViewById(R.id.feat4);
//OnClicks for news
newsb1.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
setContentView(R.layout.newstitle);
);
newsb2.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
setContentView(R.layout.newstitle);
);
newsb3.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
setContentView(R.layout.newstitle);
);
newsb4.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
setContentView(R.layout.newstitle);
);
//OnClicks for feat
featb1.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
setContentView(R.layout.newstitle);
);
featb2.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
setContentView(R.layout.newstitle);
);
featb3.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
setContentView(R.layout.newstitle);
);
featb4.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
setContentView(R.layout.newstitle);
);
rowItem = new ArrayList<RowItem>();
for(int x = 0; x < uname.length; x++)
RowItem item = new RowItem(upic[x], uname[x], ustatus[x]);
rowItem.add(item);
listview = (ListView) findViewById(R.id.sfeed);
ArrayAdapter<RowItem> adapter = new ArrayAdapter<RowItem>
(this,R.layout.sfeed_list,rowItem);
listview.setAdapter(adapter);
listview.setOnItemClickListener((OnItemClickListener)this);
@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;
我知道这需要阅读很多内容,但也许有人可以帮助我了解我的问题,而无需阅读整个日志......任何和所有帮助将不胜感激。提前感谢您的阅读和回复。
【问题讨论】:
发布MainActivity
代码....
您能否在没有代码的情况下更新您的代码,我们无法为您提供帮助。
根据上面的消息,错误位于 MainActivity 的第 23 行...这是您/我们需要查看的地方(以及之前和之后...错误并不总是关于它所在的行,但通常是前面的。)
我想你在context
初始化时尝试过访问它
请上传您的活动代码以了解确切的问题。
【参考方案1】:
检查这些都存在于您的代码中
public class youractivityname extends activity
Context context;
@override
public void Oncreate(Bundle savedInstancestate)
super.oncreate(savedInstacestate);
context=this;
//........yourcode
yourlistview.this.runOnUiThread(new runnable)
@override
public void run()
//youradapter calling with context parameter along with required parameters
yourlistview.setadapter(yourlistadapetObject);
);
然后跑,让我通知
【讨论】:
抱歉,这似乎不起作用,并导致其他错误,我已经回复了包含我的代码的答案...请看一下。谢谢。 将这些代码行放入 runOnUiThread 中它会起作用 listview = (ListView) findViewById(R.id.sfeed); ArrayAdapter以上是关于Android App 无法运行,MainActivity 出现空点异常的主要内容,如果未能解决你的问题,请参考以下文章
在Android Studio中运行Android App:无法创建密钥库
无法在 Android Auto Media App 上运行语音命令
Android App 无法运行,MainActivity 出现空点异常
Android异常篇 Gradle项目同步失败,需要修复项目。同时app图标有红叉,无法正常运行
npm i macaca-android -g 无法安装成功安装成功后运行脚本提示app-debug.apk不存在的问题