我有两个活动,都使用了Theme.Translucent主题,但是一个很好用,另一个导致错误"不幸的是,已停止工作"
这是android清单xml
<activity
android:name="com.dreama.trafic.SearchActivity"
android:theme="@android:style/Theme.Translucent">
<intent-filter >
<action android:name="com.dreama.trafic.SearchActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.dreama.trafic.ReportActivity"
android:theme="@android:style/Theme.Translucent">
<intent-filter >
<action android:name="com.dreama.trafic.ReportActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
这是ReportActivity的布局,当我将此行添加到其属性android:theme =&#34; @android:style / Theme.Translucent&#34;
时,该活动失败<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/post_edit"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/post_label"
android:background="@drawable/editable"
android:ems="10"
android:hint="Update your Traffic situation..."
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/location_edit"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/location_label"
android:background="@drawable/editable"
android:ems="10"
android:hint="Specify your location..." />
<TextView
android:id="@+id/location_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/post_edit"
android:text="Location:" />
<TextView
android:id="@+id/post_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Report:" />
<TextView
android:id="@+id/category_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/location_edit"
android:text="Choose Category:" />
<Spinner
android:id="@+id/category_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/category_label"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/cancelbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/sendbutton"
android:layout_alignBottom="@+id/sendbutton"
android:layout_alignParentRight="true"
android:layout_marginRight="24dp"
android:text="Cancel" />
<Button
android:id="@+id/sendbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/category_spinner"
android:layout_marginLeft="19dp"
android:layout_marginTop="72dp"
android:text="Send Post" />
</RelativeLayout>
这是调用活动的行
startActivity(new Intent(HomeActivity.this,ReportActivity.class));
这是我尝试调用此活动时的logcat
05-15 18:34:38.642: E/AndroidRuntime(15186): FATAL EXCEPTION: main
05-15 18:34:38.642: E/AndroidRuntime(15186): java.lang.RuntimeException:
05-15 18:34:38.642: E/AndroidRuntime(15186): java.lang.RuntimeException: Unable to
start activity ComponentInfo{com.dreama.trafic/com.dreama.trafic.ReportActivity}:
java.lang.NullPointerException
05-15 18:34:38.642: E/AndroidRuntime(15186): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
05-15 18:34:38.642: E/AndroidRuntime(15186): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
05-15 18:34:38.642: E/AndroidRuntime(15186): at )
android.app.ActivityThread.access$600(ActivityThread.java:156
05-15 18:34:38.642: E/AndroidRuntime(15186): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
05-15 18:34:38.642: E/AndroidRuntime(15186): at
android.os.Handler.dispatchMessage(Handler.java:99)
05-15 18:34:38.642: E/AndroidRuntime(15186): at
android.os.Looper.loop(Looper.java:153)
05-15 18:34:38.642: E/AndroidRuntime(15186): at
android.app.ActivityThread.main(ActivityThread.java:5297)
05-15 18:34:38.642: E/AndroidRuntime(15186): at
java.lang.reflect.Method.invokeNative(Native Method)
05-15 18:34:38.642: E/AndroidRuntime(15186): at
java.lang.reflect.Method.invoke(Method.java:511)
05-15 18:34:38.642: E/AndroidRuntime(15186): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-15 18:34:38.642: E/AndroidRuntime(15186): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-15 18:34:38.642: E/AndroidRuntime(15186): at
dalvik.system.NativeStart.main(Native
Method)
05-15 18:34:38.642: E/AndroidRuntime(15186): Caused by: java.lang.NullPointerException
05-15 18:34:38.642: E/AndroidRuntime(15186): at
com.dreama.trafic.ReportActivity.onCreate(ReportActivity.java:25)
05-15 18:34:38.642: E/AndroidRuntime(15186): at
android.app.Activity.performCreate(Activity.java:5122)
05-15 18:34:38.642: E/AndroidRuntime(15186): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-15 18:34:38.642: E/AndroidRuntime(15186): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
05-15 18:34:38.642: E/AndroidRuntime(15186): ... 11 more
这是ReportActivity.java文件
public class ReportActivity extends Activity {
String[] presidents;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.report_layout);
ActionBar actionBar = getActionBar();
actionBar.hide();
presidents = getResources().getStringArray(R.array.presidents_array);
Spinner s1 = (Spinner) findViewById(R.id.category_spinner);
ArrayAdapter<String> adapter = new
ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,
presidents);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onNothingSelected(AdapterView<?> arg0) { }
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2,
long arg3) {
// TODO Auto-generated method stub
int index = arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(),"You have selected item :
"+ presidents[index],Toast.LENGTH_SHORT).show();
}
});
}
}
应该注意的是,当我运行SearchActivity时,它工作得很好,但ReportActivity是相反的,但是如果我删除了android:主题行,那么活动就会很好地显示
答案 0 :(得分:0)
发现问题,我删除了这一行
ActionBar actionBar = getActionBar();
actionBar.hide();
并且效果非常好。