我对Android开发很新。我创建了一个名为" series"的应用。 如果我想运行我的应用程序,它会一直停止并告诉我那里有一个NullPointerException ...... 任何人都可以帮助我吗?
这是我的代码:
public class MainActivity extends AppCompatActivity {
int episode = 1;
int season = 1;
final Button add = (Button) findViewById(R.id.add);
@Override
protected void onCreate(Bundle savedInstancesState) {
super.onCreate(savedInstancesState);
setContentView(R.layout.activity_main);
ListView resultListView = (ListView) findViewById(R.id.series_list);
HashMap<String, String> nameAdresses = new HashMap<>();
nameAdresses.put("The 100 ", "Season " + season + " Episode" + episode);
nameAdresses.put("Supernatural ", "Season " + season + " Episode " + episode);
List<HashMap<String, String>> listItems = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.text1, R.id.text2});
Iterator it = nameAdresses.entrySet().iterator();
while (it.hasNext()) {
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultsMap.put("First Line", pair.getKey().toString());
resultsMap.put("Second Line", pair.getValue().toString());
listItems.add(resultsMap);
}
resultListView.setAdapter(adapter);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
episode++;
}
});
}
}
logcat的:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.yurivanderburg.series, PID: 2389
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.yurivanderburg.series/com.example.yurivanderburg.series.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:33)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.yurivanderburg.series.MainActivity.<init>(MainActivity.java:21)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:1)
请将此行放在oncreateView ()
final Button add = (Button) findViewById(R.id.add);
之后
setContentView(R.layout.activity_main);
答案 1 :(得分:1)
final Button add = (Button) findViewById(R.id.add);
删除此行并添加
Button add;
并在你的setContentView下面添加这一行
add = (Button) findViewById(R.id.add);
希望这可以帮助你
答案 2 :(得分:0)
final Button add = (Button) findViewById(R.id.add);
您在OnCreate
之外添加了此内容。这在Android中是不允许的,您无法在setContentView
之前初始化视图对象。
答案 3 :(得分:0)
在onCreate
public class MainActivity extends AppCompatActivity {
int episode = 1;
int season = 1;
@Override
protected void onCreate(Bundle savedInstancesState) {
super.onCreate(savedInstancesState);
setContentView(R.layout.activity_main);
Button add = (Button) findViewById(R.id.add);
ListView resultListView = (ListView) findViewById(R.id.series_list);
HashMap<String, String> nameAdresses = new HashMap<>();
nameAdresses.put("The 100 ", "Season " + season + " Episode" + episode);
nameAdresses.put("Supernatural ", "Season " + season + " Episode " + episode);
List<HashMap<String, String>> listItems = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.text1, R.id.text2});
Iterator it = nameAdresses.entrySet().iterator();
while (it.hasNext()) {
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultsMap.put("First Line", pair.getKey().toString());
resultsMap.put("Second Line", pair.getValue().toString());
listItems.add(resultsMap);
}
resultListView.setAdapter(adapter);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
episode++;
}
});
}
}