空指针错误

时间:2013-03-09 16:10:38

标签: android nullpointerexception runtime-error

我得到了null异常错误我需要一些帮助才能弄清楚,因为我试图更新我的Manifest并且它看起来很好但仍然得到错误

这里是logcat

03-09 16:00:53.803: E/JSON(540): {"tag":"login","success":1,"error":0,"uid":"513b5349d86546.88432881","user":{"name":"p","email":"p","created_at":"2013-03-09 23:20:41","updated_at":null}}
03-09 16:00:54.863: E/AndroidRuntime(540): FATAL EXCEPTION: main
03-09 16:00:54.863: E/AndroidRuntime(540): java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.mohammed.watzIslam/com.mohammed.watzIslam.maincontentview}: java.lang.NullPointerException
03-09 16:00:54.863: E/AndroidRuntime(540):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-09 16:00:54.863: E/AndroidRuntime(540):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-09 16:00:54.863: E/AndroidRuntime(540):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-09 16:00:54.863: E/AndroidRuntime(540):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-09 16:00:54.863: E/AndroidRuntime(540):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 16:00:54.863: E/AndroidRuntime(540):  at android.os.Looper.loop(Looper.java:123)
03-09 16:00:54.863: E/AndroidRuntime(540):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-09 16:00:54.863: E/AndroidRuntime(540):  at java.lang.reflect.Method.invokeNative(Native Method)
03-09 16:00:54.863: E/AndroidRuntime(540):  at java.lang.reflect.Method.invoke(Method.java:521)
03-09 16:00:54.863: E/AndroidRuntime(540):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-09 16:00:54.863: E/AndroidRuntime(540):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-09 16:00:54.863: E/AndroidRuntime(540):  at dalvik.system.NativeStart.main(Native Method)
03-09 16:00:54.863: E/AndroidRuntime(540): Caused by: java.lang.NullPointerException
03-09 16:00:54.863: E/AndroidRuntime(540):  at com.mohammed.watzIslam.maincontentview.onCreate(maincontentview.java:28)
03-09 16:00:54.863: E/AndroidRuntime(540):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-09 16:00:54.863: E/AndroidRuntime(540):  at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-09 16:00:54.863: E/AndroidRuntime(540):  ... 11 more

这是我无法启动的java文件

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class maincontentview extends Activity {

public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    String[] contentarray = getResources().getStringArray(R.array.contnarr);
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.maincontentview,    R.id.label1, contentarray));




 ListView lv = getListView();

 // listening to single list item on click
 lv.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> parent, View view,
       int position, long id) {
         // start different activities
       switch(position){
        case 0:
        Intent  txt = new Intent(view.getContext(),TextA.class);

          try{
            startActivity(txt);
          } catch(ActivityNotFoundException e) {
            e.printStackTrace();
          }
          break;
        case 1:
         Intent video = new Intent(view.getContext(),Videos.class);
          try{
            startActivity(video);
          }catch(ActivityNotFoundException e){
            e.printStackTrace();
          }
          break;

        default: break;

        }           



}
 });
}

    private ListView getListView() {
    // TODO Auto-generated method stub
    return null;
    }

    private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
    // TODO Auto-generated method stub

    }

 }

最后是主要的清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mohammed.watzIslam"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
     >
    <activity
        android:name=".Mymain"
        android:label="@string/title_activity_mymain" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DashboardActivity"
        android:label="@string/title_activity_mymain" >
        <intent-filter>
            <action android:name="com.mohammed.watzIslam.DashboardActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
     <!--  Login Activity -->
    <activity
        android:label="Login Account" 
        android:name=".LoginActivity"></activity>

    <!--  Register Activity -->
    <activity
        android:label="Register New Account" 
        android:name=".RegisterActivity"></activity>
    <activity
        android:label="application interface" 
        android:name=".Appinterface"></activity>
    <activity
        android:label="content list view" 
        android:name=".maincontentview"></activity>
    <activity
        android:label="forum view" 
        android:name=".Forum"></activity>

    <activity
        android:label="videos list" 
        android:name=".Videos"></activity>
    <activity
        android:label="videoa view" 
        android:name=".VideoA"></activity>
    <activity
        android:label="videob view" 
        android:name=".VideoB"></activity>


</application>

</manifest>

1 个答案:

答案 0 :(得分:1)

您正在使用Activity扩展课程,但仍在使用 getListView()

getListView()仅在使用 ListActivity 时使用。对于普通的Activity,您需要使用setContentView(View)来生成布局。

查看您的代码,我认为您应该使用 ListActivity

<强> public class maincontentview extends ListActivity