2012-09-19 39 views
0

我試圖在同一活動中製作webview和listview。 這裏是我的佈局文件:在同一活動中的webview和listview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:background="@android:color/white" > 

<com.markupartist.android.widget.ActionBar 
    android:id="@+id/actionbar" 
    style="@style/ActionBar" /> 

<WebView 
    android:id="@+id/webView1" 
    android:layout_width="match_parent" 
    android:layout_height="210dp" /> 

<ListView 
    android:id="@+id/listtest" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 

<TextView 
    android:id="@+id/tv_test2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:textSize="16sp"/> 

,這裏是我行佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/white" 
android:orientation="vertical" > 

<com.markupartist.android.widget.ActionBar 
    android:id="@+id/actionbar" 
    style="@style/ActionBar" /> 

<TextView 
    android:id="@+id/tv_test" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:textSize="16sp" > 
</TextView> 

,這裏是我的活動:

public class Tab_Bike_Information_Activity extends Activity { 
WebView mWebView; 
TextView tv; 
private ListView mainListView; 
private ArrayAdapter<String> listAdapter; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tessss); 

    final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar); 
    actionBar.setTitle(getString(R.string.app_name)); 

    int index = getIntent().getIntExtra("text", 0); 

    mWebView = (WebView) findViewById(R.id.webView1); 

    String temp = "<html><body>" + "<p align=\"justify\">" 
      + getString(R.string.xc_info + index) + "</p> " 
      + "</body></html>"; 
    mWebView.loadData(temp, "text/html", "utf-8"); 


    mainListView = (ListView) findViewById(android.R.id.list); 

    // Create and populate a List of planet names. 
    String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars", 
             "Jupiter", "Saturn", "Uranus", "Neptune"};  
    ArrayList<String> planetList = new ArrayList<String>(); 
    planetList.addAll(Arrays.asList(planets)); 

    // Create ArrayAdapter using the planet list. 
    listAdapter = new ArrayAdapter<String>(this, R.layout.tessss_row, planetList); 

    // Add more planets. If you passed a String[] instead of a List<String> 
    // into the ArrayAdapter constructor, you must not add more items. 
    // Otherwise an exception will occur. 
    listAdapter.add("Ceres"); 
    listAdapter.add("Pluto"); 
    listAdapter.add("Haumea"); 
    listAdapter.add("Makemake"); 
    listAdapter.add("Eris"); 


    tv = (TextView) findViewById(R.id.tv_test); 

} 

和這裏的logcat的:

09-19 20:04:04.659: E/AndroidRuntime(13951): FATAL EXCEPTION: main 
09-19 20:04:04.659: E/AndroidRuntime(13951): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wilis.hellotabwidget/com.wilis.hellotabwidget.Tab_Bike_Information_Activity}: java.lang.NullPointerException 
09-19 20:04:04.659: E/AndroidRuntime(13951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
09-19 20:04:04.659: E/AndroidRuntime(13951): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
09-19 20:04:04.659: E/AndroidRuntime(13951): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
09-19 20:04:04.659: E/AndroidRuntime(13951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
09-19 20:04:04.659: E/AndroidRuntime(13951): at android.os.Handler.dispatchMessage(Handler.java:99) 
09-19 20:04:04.659: E/AndroidRuntime(13951): at android.os.Looper.loop(Looper.java:130) 
09-19 20:04:04.659: E/AndroidRuntime(13951): at android.app.ActivityThread.main(ActivityThread.java:3687) 
09-19 20:04:04.659: E/AndroidRuntime(13951): at java.lang.reflect.Method.invokeNative(Native Method) 
09-19 20:04:04.659: E/AndroidRuntime(13951): at java.lang.reflect.Method.invoke(Method.java:507) 
09-19 20:04:04.659: E/AndroidRuntime(13951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
09-19 20:04:04.659: E/AndroidRuntime(13951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
09-19 20:04:04.659: E/AndroidRuntime(13951): at dalvik.system.NativeStart.main(Native Method) 
09-19 20:04:04.659: E/AndroidRuntime(13951): Caused by: java.lang.NullPointerException 
09-19 20:04:04.659: E/AndroidRuntime(13951): at com.wilis.hellotabwidget.Tab_Bike_Information_Activity.onCreate(Tab_Bike_Information_Activity.java:75) 
09-19 20:04:04.659: E/AndroidRuntime(13951): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
09-19 20:04:04.659: E/AndroidRuntime(13951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
09-19 20:04:04.659: E/AndroidRuntime(13951): ... 11 more 

任何人可以在這方面幫助?謝謝

+1

第75行有一個NullPointer異常。這是哪一行? – Thommy

+0

@Thommy'mainListView.setAdapter(listAdapter);'爲什麼? –

+0

在我的回答下方 – Thommy

回答

0

我看到兩個問題:首先是你的listview的ID。 變化

mainListView = (ListView) findViewById(android.R.id.list); 

mainListView = (ListView) findViewById(R.id.listtest); 

也許你需要刪除android.R進口。*,使日食認識的變化。

其次,在您的適配器中現在有指定的TextView。
變化

listAdapter = new ArrayAdapter<String>(this, R.layout.tessss_row, planetList); 

listAdapter = new ArrayAdapter<String>(this, R.layout.tessss_row, R.id.tv_test, planetList); 

讓一個ArrayAdapter知道其中的TextView它應該寫planetList的內容。

+0

ouh ,,我感覺很蠢,非常感謝你,這對我真的很有幫助。 :) –

0

電視必須指您的TextView:findViewById()!

+0

這裏,'tv =(TextView)findViewById(R.id.tv_test);'我已經聲明textview頂部.. –

2

我看到一些東西,但我可能是錯的,我只是看代碼,並沒有測試它。

不應該類Execution ListActivity而不是Activity?

下一件事情就是在你的類,你必須:

mainListView = (ListView) findViewById(android.R.id.list); 

但在你的XML ListView中有不同的ID:

android:id="@+id/listtest" 

我想這應該是

android:id="@+id/android:list" 

另一個是TextView的id是錯的。您正在尋找tv_test,而不是tv_test2

希望這有助於。

+0

謝謝@switchingBrains :)真的幫我很多 –

+0

沒有問題,我有在那裏也是;)你真的有你的身份證混合在那裏。祝你好運,玩得開心。 –