我正在创建一个小部件,它可以安装到任何设备或模拟器,但是当我按下小部件打开活动时,我收到此错误:
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);
答案 0 :(得分:2)
您从未向mListAppInfo
分配任何内容,因此它仍为null
,并且在您尝试访问时会出现异常。
答案 1 :(得分:2)
在执行该方法之前,您刚刚没有初始化mListAppInfo
。你在它所说的地方初始化它:mListAppInfo = (ListView)findViewById(R.id.lvApps);
尝试在之前初始化它(我的意思是,在执行方法之前尝试声明它),我希望有帮助:D
答案 2 :(得分:1)
mListAppInfo
是null
,因为您还没有在任何地方初始化它。你在这里声明
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
或以任何其他方式使用它。