我是Android的新手,我在这里按照教程http://code.tutsplus.com/tutorials/android-sdk-create-a-drawing-app-touch-interaction--mobile-19202制作一个简单的绘图应用。 一切正常,直到我在onCreateView方法中添加这两个函数
currPaint = (ImageButton)paintLayout.getChildAt(0);
currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
这里有错误的代码:
package com.example.tastycorpse;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.ImageButton;
import android.widget.LinearLayout;
@SuppressLint("ValidFragment")
public class MainActivity extends ActionBarActivity {
private DrawingView drawView;
private ImageButton currPaint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
drawView = (DrawingView)findViewById(R.id.drawing);
//get the palette and first color button
LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
currPaint = (ImageButton)paintLayout.getChildAt(0);
currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
return rootView;
}
}
//user clicked paint
public void paintClicked(View view){
//use chosen color
if(view!=currPaint){
ImageButton imgView = (ImageButton)view;
String color = view.getTag().toString();
drawView.setColor(color);
//update ui
imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
currPaint=(ImageButton)view;
}
}
}
这里是logcat
05-04 12:40:24.224: E/AndroidRuntime(2887): Caused by: java.lang.NullPointerException
05-04 12:43:14.489: E/AndroidRuntime(3048): Caused by: java.lang.NullPointerException
05-04 12:43:47.919: E/AndroidRuntime(3187): Caused by: java.lang.NullPointerException
05-04 12:43:54.607: E/AndroidRuntime(3232): Caused by: java.lang.NullPointerException
我试图关注并理解本教程的所有步骤,但我已经被困在这里......(不幸的是 - 已经停止了)。我甚至无法在旧问题上找到任何内容,因为错误太笼统了。有人可以向我解释为什么我会收到此错误吗?
答案 0 :(得分:1)
在您刚刚充气的findViewById()
上致电rootView
而不是Activity.findViewById()
。片段视图层次结构还不是活动视图层次结构的一部分,尝试从活动中查找片段的视图将返回null
。在getChildAt()
上调用null
等方法会导致NPE。
例如,更改
之类的调用drawView = (DrawingView)findViewById(R.id.drawing);
到
drawView = (DrawingView)rootView.findViewById(R.id.drawing);