0
您好我對android開發非常陌生,我正在創建一個有4個圖像按鈕的活動,並且我首先嚐試使用一個圖像按鈕來鏈接到一個新的活動,但在我甚至能夠鏈接之前該頁面,似乎我的OnClick事件給我錯誤。在OnClick事件中出現NullPointerException
從我的logcat我明白,我有一個空指針異常,在第30行拋出:imageButton1.setOnClickListener(new View.OnClickListener() {
我懷疑它可能會像這導致我的findViewById ImageButtons(不同的ID)我的佈局元素()返回null ,但嘗試,因爲我可能仍然無法找到可能導致它的錯誤。因爲我還在學習,請原諒我的理解。真的很感謝任何幫助,謝謝!
RecipesFragment.java
package com.example.sgrecipe;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.content.Intent;
public class RecipesFragment extends Fragment {
ImageButton imageButton1;
ImageButton imageButton2;
ImageButton imageButton3;
ImageButton imageButton4;
Intent intent;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_recipes_fragment, container, false);
intent = new Intent(getActivity(), FavouriteFragment.class);
final ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
imageButton1.setOnClickListener(new View.OnClickListener() { //<--error here
public void onClick(View v) {
// startActivity(intent);
}
});
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipes_fragment);
addListenerOnButton();
}
private void setContentView(int activityRecipesFragment) {
// TODO Auto-generated method stub
}
public void addListenerOnButton() {
imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
imageButton3 = (ImageButton) findViewById(R.id.imageButton3);
imageButton4 = (ImageButton) findViewById(R.id.imageButton4);
}
private ImageButton findViewById(int imagebuttonselector) {
// TODO Auto-generated method stub
return null;
};
}
activity_recipes_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/chinese_button" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/malay_button" />
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/indian_button" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/others_button" />
</TableRow>
</TableLayout>
</LinearLayout>
沒有人像我明白了,謝謝你,我現在明白了我的錯誤! – zana 2015-02-12 02:39:26