gpt4 book ai didi

java - 绘图应用程序 - 将某些功能添加到 onCreateView 时出错

转载 作者:行者123 更新时间:2023-12-01 13:01:22 25 4
gpt4 key购买 nike

我是 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;
}
}

}

这是日志猫

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

我试图遵循并理解教程的所有步骤,但我被困在这里......(不幸的是-app-已停止)。我什至找不到有关旧问题的任何内容,因为错误太笼统了。有人可以向我解释为什么我会收到此错误吗?

最佳答案

在刚刚膨胀的 rootView 上调用 findViewById(),而不是 Activity.findViewById()。 fragment View 层次结构尚未成为 Activity View 层次结构的一部分,尝试从 Activity 中查找 fragment View 将返回 null。在 null 上调用诸如 getChildAt() 之类的方法会导致 NPE。

例如,更改调用,例如

drawView = (DrawingView)findViewById(R.id.drawing);

drawView = (DrawingView)rootView.findViewById(R.id.drawing);

关于java - 绘图应用程序 - 将某些功能添加到 onCreateView 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23491867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com