我创建了一个应用程序,其中我使用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
ComponentInfo{com.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;
}
}
我知道这需要阅读很多,但也许有人可以帮助我理解我的问题是什么,而不必阅读整个日志...任何和所有的帮助将不胜感激。提前感谢您阅读和回复。
答案 0 :(得分: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);
}
});
}
}
然后跑,让我通知