我是一个不太了解编程的新手
我正在尝试字典申请,而且由于
而感到困惑01-27 06:27:57.561: E/AndroidRuntime(2439): FATAL EXCEPTION: main
01-27 06:27:57.561: E/AndroidRuntime(2439): Process: taejung.kim.dic, PID: 2439
01-27 06:27:57.561: E/AndroidRuntime(2439): java.lang.RuntimeException: Unable to start activity
ComponentInfo{taejung.kim.dic/taejung.kim.dic.DictionaryMainActivity}: java.lang.NullPointerException
01-27 06:27:57.561: E/AndroidRuntime(2439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-27 06:27:57.561: E/AndroidRuntime(2439): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-27 06:27:57.561: E/AndroidRuntime(2439): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-27 06:27:57.561: E/AndroidRuntime(2439): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-27 06:27:57.561: E/AndroidRuntime(2439): at android.os.Handler.dispatchMessage(Handler.java:102)
01-27 06:27:57.561: E/AndroidRuntime(2439): at android.os.Looper.loop(Looper.java:136)
01-27 06:27:57.561: E/AndroidRuntime(2439): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-27 06:27:57.561: E/AndroidRuntime(2439): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 06:27:57.561: E/AndroidRuntime(2439): at java.lang.reflect.Method.invoke(Method.java:515)
01-27 06:27:57.561: E/AndroidRuntime(2439): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-27 06:27:57.561: E/AndroidRuntime(2439): at 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): Caused by: java.lang.NullPointerException
01-27 06:27:57.561: E/AndroidRuntime(2439): at taejung.kim.dic.DictionaryMainActivity.onCreate(DictionaryMainActivity.java:45)
01-27 06:27:57.561: E/AndroidRuntime(2439): at android.app.Activity.performCreate(Activity.java:5231)
01-27 06:27:57.561: E/AndroidRuntime(2439): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-27 06:27:57.561: E/AndroidRuntime(2439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-27 06:27:57.561: E/AndroidRuntime(2439): ... 11 more
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>
清单
<?xml version="1.0" encoding="utf-8"?>
<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>
我认为问题出现在主要活动的OnCreate中,但我无法理解这个问题是什么。
答案 0 :(得分:1)
您忘记初始化lv
变量。添加
lv = (ListView)findViewById(R.id.listview);
在setContentView()
之后和lv
上调用方法之前。
答案 1 :(得分:0)
您的ListView
lv永远不会被初始化。所以你在这一行获得了一个NPE:
lv.setAdapter(adapter);
答案 2 :(得分:0)
您的ListView lv;
变量未初始化,因此您尝试为尚未创建的对象调用lv.setAdapter(adapter);
。