1

我正在制作一个小部件,它可以很好地安装到任何设备或模拟器上,但是当我通过按下小部件打开一个活动时,我收到此错误:

10-16 11:28:43.585: E/AndroidRuntime(14065): FATAL EXCEPTION: main
10-16 11:28:43.585: E/AndroidRuntime(14065): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.awesomefilebuilderwidget/com.example.awesomefilebuilderwidget.Drag_and_Drop_App}: java.lang.NullPointerException
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1833)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1854)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.app.ActivityThread.access$1500(ActivityThread.java:135)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1041)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.os.Looper.loop(Looper.java:150)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.app.ActivityThread.main( ActivityThread.java:4333)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at java.lang.reflect.Method.invokeNative(Native Method)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at java.lang.reflect.Method.invoke(Method.java:507)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at dalvik.system.NativeStart.main(Native Method)
10-16 11:28:43.585: E/AndroidRuntime(14065): Caused by: java.lang.NullPointerException
10-16 11:28:43.585: E/AndroidRuntime(14065):    at com.example.awesomefilebuilderwidget.Drag_and_Drop_App.onCreate(Drag_and_Drop_App.java:46)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1797)
10-16 11:28:43.585: E/AndroidRuntime(14065):    ... 11 more

它说第 46 行有一个错误,但我无法弄清楚错误是什么。这是我的编码:

package com.example.awesomefilebuilderwidget;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Drag_and_Drop_App extends Activity {
private ListView mListAppInfo;
// Search EditText
EditText inputSearch;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // set layout for the main screen
    setContentView(R.layout.drag_and_drop_app);
    // import buttons
    Button btnLinkToFeedback = (Button) findViewById(R.id.btnLinkToFeedback);

    // Link to Feedback Screen
    btnLinkToFeedback.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                    Feedback.class);
            startActivity(i);
            finish();
        }
    });
    // create new adapter
    AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
    // set adapter to list view
    mListAppInfo.setAdapter(adapter);
    // search bar
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            // Drag_and_Drop_App.this.adapter.getFilter().filter(cs);  
            // Drag_and_Drop_App.this.adapter.getFilter().filter(cs);

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub                          
        }
        });
     // load list application
    mListAppInfo = (ListView)findViewById(R.id.lvApps);

    // implement event when an item on list view is selected
    mListAppInfo.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView parent, View view, int pos, long id) {
            // get the list adapter
            AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
            // get selected item on the list
            ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
            // launch the selected application
            Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
        }

    });

    // implement event when an item on list view is selected via long-click for drag and drop
    mListAppInfo.setOnItemLongClickListener(new OnItemLongClickListener(){

        @Override
        public boolean onItemLongClick(AdapterView parent, View view,
                int pos, long id) {
            // TODO Auto-generated method stub
            // get the list adapter
            AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
            // get selected item on the list
            ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
            // launch the selected application
            Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
            return true;
        }


    });
}
}

这是第 46 行:

 mListAppInfo.setAdapter(adapter);
4

3 回答 3

2

您从未为 分配任何内容mListAppInfo,因此它仍然是null,并且在尝试访问它时会出现异常。

于 2013-10-16T17:40:03.920 回答
2

mListAppInfo您只是在对其执行方法之前没有进行初始化。你在它说的地方初始化了它:mListAppInfo = (ListView)findViewById(R.id.lvApps);

尝试在之前对其进行初始化(我的意思是,尝试在对其执行方法之前声明它),希望对您有所帮助:D

于 2013-10-16T17:41:34.500 回答
1

mListAppInfonull因为你还没有在任何地方初始化它。你在这里声明

private ListView mListAppInfo;

但你从来没有给它一个价值。就像是

setContentView(R.layout.drag_and_drop_app);
mListAppInfo = (ListView) findViewById(R.id.myLV);  // where myLV is the id of your 
                                                    // ListView in drag_and_drop_app.xml

编辑

进一步查看您的代码后,我看到您稍后再做

 mListAppInfo = (ListView)findViewById(R.id.lvApps);

但这需要您尝试设置Adapter它或以任何其他方式使用它之前完成。

于 2013-10-16T17:37:59.303 回答