Android 应用程序正在崩溃。。找到了logcat的解释但不知道为什么
代码: 我的新问题是,在Eclipse图形布局中我可以看到文本,但在我的设备上我看不到 我不知道为什么会发生这种情况,无论如何,我的文本设置为黑色,背景设置为白色,所以这不是问题。textview highscore\u int为空,因为您从fragment\u main.xml引用highscore\u int,您将得到空值,因为在您的活动中,您正在使用活动的布局 因此,在片段的onCreateView中使用highscore_int将视图充气Android 应用程序正在崩溃。。找到了logcat的解释但不知道为什么,android,layout,crash,textview,Android,Layout,Crash,Textview,代码: 我的新问题是,在Eclipse图形布局中我可以看到文本,但在我的设备上我看不到 我不知道为什么会发生这种情况,无论如何,我的文本设置为黑色,背景设置为白色,所以这不是问题。textview highscore\u int为空,因为您从fragment\u main.xml引用highscore\u int,您将得到空值,因为在您的活动中,您正在使用活动的布局 因此,在片段的onCreateView中使用highscore_int将视图充气 package com.exam
package com.example.theball;
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
@SuppressLint("NewApi") public class MainActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
private Sensor accelerometer;
@SuppressWarnings("unused")
private long lastUpdate;
AnimatedView animatedView = null;
ShapeDrawable mDrawable = new ShapeDrawable();
public static int x;
public static int y;
public static final int width = 50;
public static final int height = 50;
public boolean firstDraw = true;
private int screen_width;
private int screen_height;
private int sensorX;
private Timer t;
private int TimeCounter = 0;
private int sensorY;
private int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = sensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
lastUpdate = System.currentTimeMillis();
animatedView = new AnimatedView(this);
setContentView(animatedView);
t=new Timer();
t.scheduleAtFixedRate(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
TimeCounter++;
}
});
}
}, 0, 1000);
}
public static class MainActivity extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_main, container, false);
TextView highscore_int = (TextView)v.findViewById(R.id.highscore_int );
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey",
Context.MODE_PRIVATE);
score = prefs.getInt("key", 0);
highscore_int.setText("Highscore:" + score +" seconds.");
return v;
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
sensorY = (int) event.values[1];
sensorX = (int) event.values[0];
x -= sensorX*3;
y += sensorY*3;
if(x <= 0 || x >= screen_width || y <= 0 || y >= screen_height) {
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int oldScore = prefs.getInt("key", 0);
if( TimeCounter > oldScore ){
Editor edit = prefs.edit();
edit.putInt("key", TimeCounter);
edit.commit(); }
Intent myIntent = new Intent(this, YouLost.class);
startActivity(myIntent);
}
}
}
public class AnimatedView extends ImageView {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
static final int width = 50;
static final int height = 50;
@SuppressLint("NewApi")
public AnimatedView(Context context) {
super(context);
// TODO Auto-generated constructor stub
display.getSize(size);
screen_width = size.x;
screen_height = size.y;
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xffffAC23);
mDrawable.setBounds(0, 0, screen_width, screen_height);
}
@Override
protected void onDraw(Canvas canvas) {
mDrawable.setBounds(x, y, x + width, y + height);
if(firstDraw) {
x = screen_width / 2;
y = screen_height / 2;
firstDraw = false;
}
mDrawable.draw(canvas);
invalidate();
}
}
}
然后使用视图查找textview
View v = inflater.inflate(R.layout.fragment_main, container, false);
i、 e
我的猜测基于单词fragment_main.xml。我猜您粘贴的代码来自您的活动,布局基于您的片段。这意味着您只能在片段类中调用findViewById 重写片段方法onCreateView并在那里获得highscore文本视图。将代码也移到那里:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
TextView highscore_int = (TextView)v.findViewById(R.id.highscore_int );
SharedPreferences prefs = getActivity().getSharedPreferences("myPrefsKey",
Context.MODE_PRIVATE);
score = prefs.getInt("key", 0);
highscore_int.setText("Highscore:" + score +" seconds.");
return v;
}
因为prefs.getIntkey为0;正在返回null并存储在score中
用这个
您也可以粘贴布局文件吗?您的文本视图似乎为空。请检查主活动行76。null值获得通过。显示完整的onCreate代码这是一种简洁的表示形式,您是否可以发布xml及其名称,确保onCreate中的setContentView引用了与主活动相同的onepaste第76行。很抱歉,它不起作用,行“ifprefs.getIntkey,0!=无效的{'获取一个错误,但没有可用的建议。View无法解析为变量,另外,它要求我删除覆盖。很抱歉,mView来自我复制的代码。我将编辑代码。对于覆盖:您可能希望检查您的Fragment类中是否有它,并且您的Java级别为6,否则您将收到一条消息,即ba基于Java 4 afaik的sed。它正在清除错误,但仍然没有响应,因为其他原因:@user3784668发布带有新问题的新问题,如果现有问题已解决,则接受答案。是的,谢谢:一个半小时到下一个问题:嗯,我现在有一个其他问题..在我的Eclipse图形布局中,我可以看到文本视图,但我无法看到在我的设备上编辑它…我将文本设置为12312,但在我的设备中,您将什么也看不到。@user3784668使用oncreate活动方法编辑您的问题。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
TextView highscore_int = (TextView)v.findViewById(R.id.highscore_int );
SharedPreferences prefs = getActivity().getSharedPreferences("myPrefsKey",
Context.MODE_PRIVATE);
score = prefs.getInt("key", 0);
highscore_int.setText("Highscore:" + score +" seconds.");
return v;
}
//onCreateView() of your fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, null);
highscore_int = (TextView) view.findViewById(R.id.highscore_int);
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
score = prefs.getInt("key", 0);
highscore_int.setText("Highscore:" + score +" seconds.");
// You lost
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int oldScore = prefs.getInt("key", 0);
if( TimeCounter > oldScore ) {
Editor edit = prefs.edit();
edit.putInt("key", TimeCounter);
edit.commit();
}
}
score = 0;
if(prefs.getInt("key", 0) != null){
score = prefs.getInt("key", 0);
}