问题描述
- 新人求助 java.lang.NullPointerException
-
04-26 21:42:40.039: E/AndroidRuntime(1125): FATAL EXCEPTION: main
04-26 21:42:40.039: E/AndroidRuntime(1125): Process: com.example.remotecontrol, PID: 1125
04-26 21:42:40.039: E/AndroidRuntime(1125): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.remotecontrol/com.example.remotecontrol.RemoteControlActivity}: java.lang.NullPointerException
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.app.ActivityThread.access$700(ActivityThread.java:135)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.os.Handler.dispatchMessage(Handler.java:102)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.os.Looper.loop(Looper.java:137)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-26 21:42:40.039: E/AndroidRuntime(1125): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 21:42:40.039: E/AndroidRuntime(1125): at java.lang.reflect.Method.invoke(Method.java:515)
04-26 21:42:40.039: E/AndroidRuntime(1125): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-26 21:42:40.039: E/AndroidRuntime(1125): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-26 21:42:40.039: E/AndroidRuntime(1125): at dalvik.system.NativeStart.main(Native Method)
04-26 21:42:40.039: E/AndroidRuntime(1125): Caused by: java.lang.NullPointerException
04-26 21:42:40.039: E/AndroidRuntime(1125): at com.example.remotecontrol.RemoteControlFragment.onCreateView(RemoteControlFragment.java:43)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.app.Activity.performStart(Activity.java:5253)
04-26 21:42:40.039: E/AndroidRuntime(1125): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2149)
04-26 21:42:40.039: E/AndroidRuntime(1125): ... 11 more
解决方案
。。。。。。我知道了= = 我傻逼吧 i 打成 1 了
解决方案二:
这是第43行: for(int j = 0; j < row.getChildCount(); j++){
解决方案三:
我晕,这是空指针异常,不贴出代码,别人怎么找错误 !
解决方案四:
04-26 21:42:40.039: E/AndroidRuntime(1125): Caused by: java.lang.NullPointerException
04-26 21:42:40.039: E/AndroidRuntime(1125): at com.example.remotecontrol.RemoteControlFragment.onCreateView(RemoteControlFragment.java:43)
解决方案五:
代码:
RemoteControlFragment:
package com.example.remotecontrol;
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.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class RemoteControlFragment extends Fragment {
private TextView mSelectedTextView;
private TextView mWorkingTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_remote_control, container,false);
mSelectedTextView = (TextView)v.findViewById(R.id.fragment_remote_control_selectedtextView);
mWorkingTextView = (TextView)v.findViewById(R.id.fragment_remote_control_workingTextView);
View.OnClickListener numberButtonListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView textView = (TextView)v;
String working = mWorkingTextView.getText().toString();
String text = textView.getText().toString();
if(working.equals("0")){
mWorkingTextView.setText(text);
} else{
mWorkingTextView.setText(working + text);
}
}
};
TableLayout tableLayout = (TableLayout)v.findViewById(R.id.fragment_remote_control_tableLayout);
int number = 1;
for(int i = 2; 1 < tableLayout.getChildCount() - 1; i++){
TableRow row = (TableRow)tableLayout.getChildAt(i);
for(int j = 0; j < row.getChildCount(); j++){
Button button = (Button)row.getChildAt(j);
button.setText("" + number);
button.setOnClickListener(numberButtonListener);
number++;
}
}
TableRow bottomRow = (TableRow)tableLayout.getChildAt(tableLayout.getChildCount() - 1);
Button deleteButton = (Button)bottomRow.getChildAt(0);
deleteButton.setText("Delete");
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mWorkingTextView.setText("0");
}
});
Button zeroButton = (Button)bottomRow.getChildAt(1);
zeroButton.setText("0");
zeroButton.setOnClickListener(numberButtonListener);
Button enterButton =(Button)bottomRow.getChildAt(2);
enterButton.setText("Enter");
enterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
CharSequence working = mWorkingTextView.getText();
if(working.length() > 0){
mSelectedTextView.setText(working);
mWorkingTextView.setText("0");
}
}
});
return v;
}
}
解决方案六:
RemoteControlFragment.java 第43行有一个对象没赋值就用了
解决方案七:
43行是哪句 你粘出来
时间: 2025-01-08 10:23:56