我对android还很陌生,所以如果这是一个完全的菜鸟问题,我深表歉意。我花了几个小时寻找类似的主题,但我找不到解决方案。
尝试运行应用程序时出现此错误:
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.avnsoft.runtracker/com.avnsoft.runtracker.RunActivity}:
> java.lang.NullPointerException
堆栈跟踪:
> AndroidRuntime(21828): FATAL EXCEPTION: main 06-07 00:35:24.792:
> E/AndroidRuntime(21828): java.lang.RuntimeException: Unable to start
> activity
> ComponentInfo{com.avnsoft.runtracker/com.avnsoft.runtracker.RunActivity}:
> java.lang.NullPointerException 06-07 00:35:24.792:
> E/AndroidRuntime(21828): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
> 06-07 00:35:24.792: E/AndroidRuntime(21828): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
> 06-07 00:35:24.792: E/AndroidRuntime(21828): at
> android.app.ActivityThread.access$600(ActivityThread.java:141) 06-07
> 00:35:24.792: E/AndroidRuntime(21828): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
> 06-07 00:35:24.792: E/AndroidRuntime(21828): at
> android.os.Handler.dispatchMessage(Handler.java:99) 06-07
> 00:35:24.792: E/AndroidRuntime(21828): at
> android.os.Looper.loop(Looper.java:137) 06-07 00:35:24.792:
> E/AndroidRuntime(21828): at
> android.app.ActivityThread.main(ActivityThread.java:5041) 06-07
> 00:35:24.792: E/AndroidRuntime(21828): at
> java.lang.reflect.Method.invokeNative(Native Method) 06-07
> 00:35:24.792: E/AndroidRuntime(21828): at
> java.lang.reflect.Method.invoke(Method.java:511) 06-07 00:35:24.792:
> E/AndroidRuntime(21828): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
> 06-07 00:35:24.792: E/AndroidRuntime(21828): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-07
> 00:35:24.792: E/AndroidRuntime(21828): at
> dalvik.system.NativeStart.main(Native Method) 06-07 00:35:24.792:
> E/AndroidRuntime(21828): Caused by: java.lang.NullPointerException
> 06-07 00:35:24.792: E/AndroidRuntime(21828): at
> android.view.ViewGroup.addViewInner(ViewGroup.java:3338) 06-07
> 00:35:24.792: E/AndroidRuntime(21828): at
> android.view.ViewGroup.addView(ViewGroup.java:3210) 06-07
> 00:35:24.792: E/AndroidRuntime(21828): at
> android.view.ViewGroup.addView(ViewGroup.java:3186) 06-07
> 00:35:24.792: E/AndroidRuntime(21828): at
> com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:289)
> 06-07 00:35:24.792: E/AndroidRuntime(21828): at
> com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:279)
> 06-07 00:35:24.792: E/AndroidRuntime(21828): at
> android.app.Activity.setContentView(Activity.java:1901) 06-07
> 00:35:24.792: E/AndroidRuntime(21828): at
> com.avnsoft.runtracker.SingleFragmentActivity.onCreate(SingleFragmentActivity.java:21)
> 06-07 00:35:24.792: E/AndroidRuntime(21828): at
> com.avnsoft.runtracker.RunActivity.onCreate(RunActivity.java:11) 06-07
> 00:35:24.792: E/AndroidRuntime(21828): at
> android.app.Activity.performCreate(Activity.java:5104) 06-07
> 00:35:24.792: E/AndroidRuntime(21828): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
> 06-07 00:35:24.792: E/AndroidRuntime(21828): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
> 06-07 00:35:24.792: E/AndroidRuntime(21828): ... 11 more
活动片段.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
活动运行.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".RunActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
片段运行.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="5sp"
android:text="@string/started"
/>
<TextView android:id="@+id/run_startedTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="5sp"
android:text="@string/latitude"
/>
<TextView android:id="@+id/run_latitudeTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="5sp"
android:text="@string/longitude"
/>
<TextView android:id="@+id/run_longitudeTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="5sp"
android:text="@string/altitude"
/>
<TextView android:id="@+id/run_altitudeTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="5sp"
android:text="@string/elapsed_time"
/>
<TextView android:id="@+id/run_durationTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
/>
</TableRow>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
>
<Button android:id="@+id/run_startButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/start"
/>
<Button android:id="@+id/run_stopButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/stop"
/>
</LinearLayout>
</TableLayout>
SingleFragmentActivity.java:
package com.avnsoft.runtracker;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.widget.FrameLayout;
public abstract class SingleFragmentActivity extends FragmentActivity {
protected static final String FRAGMENT_TAG = "SingleFragmentActivity.Fragment";
protected abstract Fragment createFragment();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout fl = new FrameLayout(this);
fl.setId(R.id.fragmentContainer);
setContentView(fl);
FragmentManager manager = getSupportFragmentManager();
Fragment fragment = manager.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
fragment = createFragment();
manager.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
}
}
}
运行活动.java:
package com.avnsoft.runtracker;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Menu;
public class RunActivity extends SingleFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_run);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.run, menu);
return true;
}
@Override
protected Fragment createFragment() {
return new RunFragment();
}
}
最后是 RunFragment.java:
package com.avnsoft.runtracker;
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.TextView;
public class RunFragment extends Fragment {
private Button mStartButton, mStopButton;
private TextView mStartedTextView,
mLatitudeTextView,
mLongitudeTextView,
mAltitudetextView,
mDurationTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Create View
View view = inflater.inflate(R.layout.fragment_run, container, false);
//Assign widgets
mStartedTextView = (TextView)view.findViewById(R.id.run_startedTextView);
mLatitudeTextView = (TextView)view.findViewById(R.id.run_latitudeTextView);
mLongitudeTextView = (TextView)view.findViewById(R.id.run_longitudeTextView);
mAltitudetextView = (TextView)view.findViewById(R.id.run_altitudeTextView);
mDurationTextView = (TextView)view.findViewById(R.id.run_durationTextView);
mStartButton = (Button)view.findViewById(R.id.run_startButton);
mStopButton = (Button)view.findViewById(R.id.run_stopButton);
return view;
}
}
我为过多的代码道歉,但在花了 4 个小时试图找到问题后,我不知道它在哪里......有人可以帮忙吗?谢谢!