setcontentview()不适用于活动

时间:2011-04-10 11:41:59

标签: android android-intent broadcastreceiver

在我的应用程序中,我有广播接收器和活动。

每当拨打电话时,广播接收机接收并向活动发送意图。我从接收器开始这项活动。当我正在进行呼叫时此活动正在启动但该视图未在该活动中设置。我正在使用setContentView()

CallReciever.java

public class CallReceiver extends BroadcastReceiver
{     @Override
public void onReceive(Context context, Intent intent)
{


String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Bundle bundle1 = new Bundle();
bundle1.putString("phone_number", phoneNumber);
Intent intent1 = new Intent();
intent1.setClass(context, NewActivity.class);
intent1.putExtras(bundle1);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.setAction("NewActivity");
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent1);

NewActivity.java

public class NewActivity extends Activity {
@Override
 public void onNewIntent(Intent intent){
    super.onNewIntent(intent);
    Bundle bundle = getIntent().getExtras();
    String phoneNumber =  bundle.getString("phone_number");
  setContentView(R.layout.temp);

     <activity android:name=".NewActivity"
     android:launchMode="singleTop"
     android:label="@string/app_name1">

     <intent-filter>
     <action android:name="NewActivity"/>
     </intent-filter>

@Override
public void onCreate(Bundle savedInstanceState) {
Bundle bundle = getIntent().getExtras();
String phoneNumber =  bundle.getString("phone_number");

 setContentView(R.layout.temp);
}   }

我也覆盖了onCreate方法。现在我得到ForceClose Dialog。我应该怎么做。?

关于layout.I使用普通的线性布局和文本视图。

2 个答案:

答案 0 :(得分:3)

您尚未正确覆盖onCreate()的{​​{1}}方法。 你应该像这样写NewActivity

NewActivity

此外,即使视图没有正确显示,我也要求您正确检查public class NewActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.temp); Bundle bundle = getIntent().getExtras(); String phoneNumber = bundle.getString("phone_number"); } } 文件以及设计模式下的R文件。我希望这可以解决您的问题。

答案 1 :(得分:0)

我猜你没有在onCreate

中覆盖NewActivity 仅在RE_LAUNCH的情况下才调用

onNewIntent。这是第一次调用onCreate

检查documentation for onNewIntent

只需覆盖onCreate,获得intent,extra,set layout等(非常类似于你的onNewIntent)

相关问题