【发布时间】:2015-01-27 11:57:22
【问题描述】:
我正在尝试字典应用程序,但收到以下错误;
01-27 06:27:57.561:E/AndroidRuntime(2439):致命异常:主要 01-27 06:27:57.561: E/AndroidRuntime(2439): 进程: taejung.kim.dic, PID: 2439 01-27 06:27:57.561: E/AndroidRuntime(2439): java.lang.RuntimeException: 无法启动活动 组件信息{taejung.kim.dic/taejung.kim.dic.DictionaryMainActivity}:java.lang.NullPointerException 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 android.app.ActivityThread.access$800(ActivityThread.java:135) 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 android.os.Handler.dispatchMessage(Handler.java:102) 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 android.os.Looper.loop(Looper.java:136) 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 android.app.ActivityThread.main(ActivityThread.java:5017) 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 java.lang.reflect.Method.invokeNative(Native Method) 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 java.lang.reflect.Method.invoke(Method.java:515) 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 01-27 06:27:57.561: E/AndroidRuntime(2439): at dalvik.system.NativeStart.main(Native Method) 01-27 06:27:57.561: E/AndroidRuntime(2439): 由: java.lang.NullPointerException 引起 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 taejung.kim.dic.DictionaryMainActivity.onCreate(DictionaryMainActivity.java:45) 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 android.app.Activity.performCreate(Activity.java:5231) 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 01-27 06:27:57.561: E/AndroidRuntime(2439): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 01-27 06:27:57.561: E/AndroidRuntime(2439): ... 11 更多
这个问题可能是由主活动中的 OnCreate 触发的,但我不明白问题是什么。
下面是我的脚本;
DictionaryMainActivity.java
package taejung.kim.dic;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
`enter code here`
public class DictionaryMainActivity extends ActionBarActivity {
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_main);
// Listview Data
String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE", "iPhone 4S", "Samsung Galaxy Note 800", "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};
// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
lv.setAdapter(adapter);
}
}
activity_list_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="taejung.kim.dic.DictionaryMainActivity">
<!-- 모음리스트 -->
<HorizontalScrollView android:id="@+id/MoumScroll" android:layout_width="match_parent" android:layout_height="wrap_content" android:fillViewport="true">
<LinearLayout android:id="@+id/MoumList" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="ㅏ" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="ㅑ" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="ㅓ" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="ㅕ" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="ㅗ" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="ㅛ" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="ㅜ" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="ㅠ" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="ㅡ" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="ㅣ" />
</LinearLayout>
</HorizontalScrollView>
<!-- 자음리스트 -->
<HorizontalScrollView android:id="@+id/JaumScroll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:fillViewport="true">
<LinearLayout android:id="@+id/JaumList" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/ga" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/na" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/da" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/ra" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/ma" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/ba" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/sa" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/aa" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/ja" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/cha" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/ka" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/ta" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/pa" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/ha" />
</LinearLayout>
</HorizontalScrollView>
<!--화면이동 -->
<LinearLayout android:id="@+id/BtLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="8">
<Button android:id="@+id/BtHelp1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="도움말" android:layout_weight="1" />
<Button android:id="@+id/BtSearch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="검색" android:layout_weight="1" />
</LinearLayout>
<ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<!-- Single ListItem -->
<!-- Product Name -->
<TextView android:id="@+id/product_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dip" android:textSize="16dip" android:textStyle="bold" />
</LinearLayout>
清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="taejung.kim.dic" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
<activity android:name=".DictionaryMainActivity" android:label="@string/app_name" android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我做错了什么,我该如何解决?
【问题讨论】: