Problem beim Starten der Activity

  • Antworten:23
  • OffenNicht stickyNicht beantwortet
  • Forum-Beiträge: 16

02.04.2014, 20:50:22 via Website

Wenn ich neue Activity starten will bekomme ich einen Fehlermeldung und der App wird gestoppt. 'App wird angehalten'

MainActivity.java
1package com.example.myfirstapp;
2
3import android.content.Intent;
4import android.os.Bundle;
5import android.support.v4.app.Fragment;
6import android.support.v7.app.ActionBarActivity;
7import android.view.LayoutInflater;
8import android.view.Menu;
9import android.view.MenuItem;
10import android.view.View;
11import android.view.ViewGroup;
12import android.widget.EditText;
13
14public class MainActivity extends ActionBarActivity {
15
16 public final static String EXTRA_MSG = "com.example.myfirstapp.MESSAGE";
17
18 @Override
19 protected void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.activity_main);
22
23 if (savedInstanceState == null) {
24 getSupportFragmentManager().beginTransaction()
25 .add(R.id.container, new PlaceholderFragment())
26 .commit();
27 }
28 }
29
30 public void sendMessage(View view) {
31 Intent intent = new Intent(this, DisplayMessage.class);
32 EditText editText = (EditText) findViewById(R.id.editText1);
33 String message = editText.getText().toString();
34 intent.putExtra(EXTRA_MSG, message);
35 startActivity(intent);
36 }
37
38
39 @Override
40 public boolean onCreateOptionsMenu(Menu menu) {
41
42 // Inflate the menu; this adds items to the action bar if it is present.
43 getMenuInflater().inflate(R.menu.main, menu);
44 return true;
45 }
46
47 @Override
48 public boolean onOptionsItemSelected(MenuItem item) {
49 // Handle action bar item clicks here. The action bar will
50 // automatically handle clicks on the Home/Up button, so long
51 // as you specify a parent activity in AndroidManifest.xml.
52 int id = item.getItemId();
53 if (id == R.id.action_settings) {
54 return true;
55 }
56 return super.onOptionsItemSelected(item);
57 }
58
59 /**
60 * A placeholder fragment containing a simple view.
61 */
62 public static class PlaceholderFragment extends Fragment {
63
64 public PlaceholderFragment() {
65 }
66
67 @Override
68 public View onCreateView(LayoutInflater inflater, ViewGroup container,
69 Bundle savedInstanceState) {
70 View rootView = inflater.inflate(R.layout.fragment_main, container, false);
71 return rootView;
72 }
73 }
74
75}

activity_main.xml

1<FrameLayout xmlns:android="XXX"
2 xmlns:tools="XXX"
3 android:id="@+id/container"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 tools:context="com.example.myfirstapp.MainActivity"
7 tools:ignore="MergeRootFrame" >
8
9
10 <LinearLayout
11 android:layout_width="match_parent"
12 android:layout_height="match_parent"
13 android:orientation="horizontal" >
14
15 <EditText
16 android:id="@+id/editText1"
17 android:layout_weight="1"
18 android:layout_width="0dp"
19 android:layout_height="wrap_content"
20 android:hint="@string/your_text" >
21
22 </EditText>
23
24 <Button
25 android:id="@+id/button1"
26 android:layout_width="wrap_content"
27 android:layout_height="wrap_content"
28 android:text="@string/button_send"
29 android:onClick="sendMessage" />
30
31 </LinearLayout>
32
33</FrameLayout>

DisplayMessage.java
1package com.example.myfirstapp;
2
3import android.support.v7.app.ActionBarActivity;
4import android.support.v7.app.ActionBar;
5import android.support.v4.app.Fragment;
6import android.content.Intent;
7import android.os.Bundle;
8import android.view.LayoutInflater;
9import android.view.Menu;
10import android.view.MenuItem;
11import android.view.View;
12import android.view.ViewGroup;
13import android.widget.TextView;
14import android.os.Build;
15
16public class DisplayMessage extends ActionBarActivity {
17
18 @Override
19 protected void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.fragment_display_message);
22
23 Intent intent = getIntent();
24 String message = intent.getStringExtra(MainActivity.EXTRA_MSG);
25
26 TextView textView = new TextView(this);
27 textView.setTextSize(40);
28 textView.setText(message);
29
30 setContentView(textView);
31
32 if (savedInstanceState == null) {
33 getSupportFragmentManager().beginTransaction()
34 .add(R.id.container, new PlaceholderFragment()).commit();
35 }
36 }
37
38 @Override
39 public boolean onCreateOptionsMenu(Menu menu) {
40
41 // Inflate the menu; this adds items to the action bar if it is present.
42 getMenuInflater().inflate(R.menu.display_message, menu);
43 return true;
44 }
45
46 @Override
47 public boolean onOptionsItemSelected(MenuItem item) {
48 // Handle action bar item clicks here. The action bar will
49 // automatically handle clicks on the Home/Up button, so long
50 // as you specify a parent activity in AndroidManifest.xml.
51 int id = item.getItemId();
52 if (id == R.id.action_settings) {
53 return true;
54 }
55 return super.onOptionsItemSelected(item);
56 }
57
58 /**
59 * A placeholder fragment containing a simple view.
60 */
61 public static class PlaceholderFragment extends Fragment {
62
63 public PlaceholderFragment() {
64 }
65
66 @Override
67 public View onCreateView(LayoutInflater inflater, ViewGroup container,
68 Bundle savedInstanceState) {
69 View rootView = inflater.inflate(R.layout.fragment_display_message,
70 container, false);
71 return rootView;
72 }
73 }
74
75}

Manifest
1<?xml version="1.0" encoding="utf-8"?>
2<manifest xmlns:android="XXX"
3 package="com.example.myfirstapp"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="8"
9 android:targetSdkVersion="19" />
10
11 <application
12 android:allowBackup="true"
13 android:icon="@drawable/ic_launcher"
14 android:label="@string/app_name"
15 android:theme="@style/AppTheme" >
16 <activity
17 android:name="com.example.myfirstapp.MainActivity"
18 android:label="@string/app_name" >
19 <intent-filter>
20 <action android:name="android.intent.action.MAIN" />
21
22 <category android:name="android.intent.category.LAUNCHER" />
23 </intent-filter>
24 </activity>
25 <activity
26 android:name="com.example.myfirstapp.DisplayMessage"
27 android:label="@string/title_activity_display_message"
28 android:parentActivityName="com.example.myfirstapp.MainActivity" >
29 <meta-data
30 android:name="android.support.PARENT_ACTIVITY"
31 android:value="com.example.myfirstapp.MainActivity" />
32 </activity>
33 </application>
34
35</manifest>

Antworten
  • Forum-Beiträge: 16

03.04.2014, 16:08:55 via Website

Das App schließt sich, wenn ich jedesmal auf den Button klicke.

LogCat.txt
104-03 16:06:07.455: D/libEGL(27602): loaded /system/lib/egl/libEGL_mali.so
204-03 16:06:07.465: D/libEGL(27602): loaded /system/lib/egl/libGLESv1_CM_mali.so
304-03 16:06:07.470: D/libEGL(27602): loaded /system/lib/egl/libGLESv2_mali.so
404-03 16:06:07.475: E/(27602): Device driver API match
504-03 16:06:07.475: E/(27602): Device driver API version: 23
604-03 16:06:07.475: E/(27602): User space API version: 23
704-03 16:06:07.475: E/(27602): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct 9 21:05:57 KST 2013
804-03 16:06:07.555: D/OpenGLRenderer(27602): Enabling debug mode 0
904-03 16:06:43.630: E/FragmentManager(27602): No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{4242c720 #0 id=0x7f05003c}
1004-03 16:06:43.630: E/FragmentManager(27602): Activity state:
1104-03 16:06:43.635: D/FragmentManager(27602): Local FragmentActivity 4241a530 State:
1204-03 16:06:43.635: D/FragmentManager(27602): mCreated=truemResumed=false mStopped=false mReallyStopped=false
1304-03 16:06:43.635: D/FragmentManager(27602): mLoadersStarted=false
1404-03 16:06:43.635: D/FragmentManager(27602): Active Fragments in 4241a798:
1504-03 16:06:43.635: D/FragmentManager(27602): #0: PlaceholderFragment{4242c720 #0 id=0x7f05003c}
1604-03 16:06:43.635: D/FragmentManager(27602): mFragmentId=#7f05003c mContainerId=#7f05003c mTag=null
1704-03 16:06:43.635: D/FragmentManager(27602): mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
1804-03 16:06:43.635: D/FragmentManager(27602): mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
1904-03 16:06:43.635: D/FragmentManager(27602): mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
2004-03 16:06:43.635: D/FragmentManager(27602): mRetainInstance=false mRetaining=false mUserVisibleHint=true
2104-03 16:06:43.635: D/FragmentManager(27602): mFragmentManager=FragmentManager{4241a798 in DisplayMessage{4241a530}}
2204-03 16:06:43.635: D/FragmentManager(27602): mActivity=com.example.myfirstapp.DisplayMessage@4241a530
2304-03 16:06:43.635: D/FragmentManager(27602): Added Fragments:
2404-03 16:06:43.635: D/FragmentManager(27602): #0: PlaceholderFragment{4242c720 #0 id=0x7f05003c}
2504-03 16:06:43.635: D/FragmentManager(27602): FragmentManager misc state:
2604-03 16:06:43.635: D/FragmentManager(27602): mActivity=com.example.myfirstapp.DisplayMessage@4241a530
2704-03 16:06:43.640: D/FragmentManager(27602): mContainer=android.support.v4.app.FragmentActivity$2@4241a810
2804-03 16:06:43.640: D/FragmentManager(27602): mCurState=2 mStateSaved=false mDestroyed=false
2904-03 16:06:43.640: D/FragmentManager(27602): View Hierarchy:
3004-03 16:06:43.640: D/FragmentManager(27602): com.android.internal.policy.impl.PhoneWindow$DecorView{4241cc18 V.E..... ... 0,0-0,0}
3104-03 16:06:43.645: D/FragmentManager(27602): com.android.internal.widget.ActionBarOverlayLayout{4241d478 V.E..... ... 0,0-0,0 #1020461 android:id/action_bar_overlay_layout}
3204-03 16:06:43.645: D/FragmentManager(27602): android.widget.FrameLayout{4241e208 V.E..... ... 0,0-0,0 #1020002 android:id/content}
3304-03 16:06:43.645: D/FragmentManager(27602): android.widget.TextView{4242bda8 V.ED.... ... 0,0-0,0}
3404-03 16:06:43.645: D/FragmentManager(27602): android.widget.LinearLayout{4241e760 V.E..... ... 0,0-0,0 #1020462 android:id/top_action_bar}
3504-03 16:06:43.645: D/FragmentManager(27602): com.android.internal.widget.ActionBarContainer{4241ec90 V.ED.... ... 0,0-0,0 #1020463 android:id/action_bar_container}
3604-03 16:06:43.645: D/FragmentManager(27602): com.android.internal.widget.ActionBarView{4241f378 V.E..... ... 0,0-0,0 #1020464 android:id/action_bar}
3704-03 16:06:43.645: D/FragmentManager(27602): android.widget.LinearLayout{42420830 VFE...C. ... 0,0-0,0}
3804-03 16:06:43.645: D/FragmentManager(27602): com.android.internal.widget.ActionBarView$HomeView{42422070 V.E..... ... 0,0-0,0}
3904-03 16:06:43.645: D/FragmentManager(27602): android.widget.ImageView{42422630 V.ED.... ... 0,0-0,0 #1020294 android:id/up}
4004-03 16:06:43.645: D/FragmentManager(27602): android.widget.ImageView{42422a10 V.ED.... ... 0,0-0,0 #102002c android:id/home}
4104-03 16:06:43.645: D/FragmentManager(27602): android.widget.LinearLayout{424245d0 G.E..... ... 0,0-0,0}
4204-03 16:06:43.645: D/FragmentManager(27602): android.widget.ImageView{42424ab8 G.ED.... ... 0,0-0,0 #1020294 android:id/up}
4304-03 16:06:43.645: D/FragmentManager(27602): android.widget.LinearLayout{42424ea0 V.E..... ... 0,0-0,0}
4404-03 16:06:43.650: D/FragmentManager(27602): android.widget.TextView{42425390 V.ED.... ... 0,0-0,0 #10202e3 android:id/action_bar_title}
4504-03 16:06:43.650: D/FragmentManager(27602): android.widget.TextView{42426408 G.ED.... ... 0,0-0,0 #10202e4 android:id/action_bar_subtitle}
4604-03 16:06:43.650: D/FragmentManager(27602): com.android.internal.widget.ActionBarContextView{42429138 G.E..... ... 0,0-0,0 #1020465 android:id/action_context_bar}
4704-03 16:06:43.650: D/FragmentManager(27602): android.widget.ImageView{424297e0 V.ED.... ... 0,0-0,0}
4804-03 16:06:43.650: D/FragmentManager(27602): com.android.internal.widget.ActionBarContainer{42429ba8 G.ED.... ... 0,0-0,0 #1020466 android:id/split_action_bar}
4904-03 16:06:43.655: D/AndroidRuntime(27602): Shutting down VM
5004-03 16:06:43.655: W/dalvikvm(27602): threadid=1: thread exiting with uncaught exception (group=0x41971700)
5104-03 16:06:43.665: E/AndroidRuntime(27602): FATAL EXCEPTION: main
5204-03 16:06:43.665: E/AndroidRuntime(27602): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessage}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{4242c720 #0 id=0x7f05003c}
5304-03 16:06:43.665: E/AndroidRuntime(27602): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
5404-03 16:06:43.665: E/AndroidRuntime(27602): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
5504-03 16:06:43.665: E/AndroidRuntime(27602): at android.app.ActivityThread.access$700(ActivityThread.java:159)
5604-03 16:06:43.665: E/AndroidRuntime(27602): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
5704-03 16:06:43.665: E/AndroidRuntime(27602): at android.os.Handler.dispatchMessage(Handler.java:99)
5804-03 16:06:43.665: E/AndroidRuntime(27602): at android.os.Looper.loop(Looper.java:176)
5904-03 16:06:43.665: E/AndroidRuntime(27602): at android.app.ActivityThread.main(ActivityThread.java:5419)
6004-03 16:06:43.665: E/AndroidRuntime(27602): at java.lang.reflect.Method.invokeNative(Native Method)
6104-03 16:06:43.665: E/AndroidRuntime(27602): at java.lang.reflect.Method.invoke(Method.java:525)
6204-03 16:06:43.665: E/AndroidRuntime(27602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
6304-03 16:06:43.665: E/AndroidRuntime(27602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
6404-03 16:06:43.665: E/AndroidRuntime(27602): at dalvik.system.NativeStart.main(Native Method)
6504-03 16:06:43.665: E/AndroidRuntime(27602): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{4242c720 #0 id=0x7f05003c}
6604-03 16:06:43.665: E/AndroidRuntime(27602): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
6704-03 16:06:43.665: E/AndroidRuntime(27602): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
6804-03 16:06:43.665: E/AndroidRuntime(27602): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
6904-03 16:06:43.665: E/AndroidRuntime(27602): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
7004-03 16:06:43.665: E/AndroidRuntime(27602): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
7104-03 16:06:43.665: E/AndroidRuntime(27602): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1188)
7204-03 16:06:43.665: E/AndroidRuntime(27602): at android.app.Activity.performStart(Activity.java:5382)
7304-03 16:06:43.665: E/AndroidRuntime(27602): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
7404-03 16:06:43.665: E/AndroidRuntime(27602): ... 11 more

Antworten
Pascal P.
  • Mod
  • Blogger
  • Forum-Beiträge: 10.165

03.04.2014, 17:20:14 via Website

[code] No view found for id[/code
Es wird das Main Layout nicht gefunden

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
  • Forum-Beiträge: 16

03.04.2014, 17:38:25 via Website

fragment_display_message.xml
1<RelativeLayout xmlns:android="XXX"
2 xmlns:tools="XXX"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:paddingBottom="@dimen/activity_vertical_margin"
6 android:paddingLeft="@dimen/activity_horizontal_margin"
7 android:paddingRight="@dimen/activity_horizontal_margin"
8 android:paddingTop="@dimen/activity_vertical_margin"
9 tools:context="com.example.myfirstapp.DisplayMessage$PlaceholderFragment" >
10
11 <TextView
12 android:layout_width="wrap_content"
13 android:layout_height="wrap_content"
14 android:text="@string/final_text" />
15
16</RelativeLayout>

Antworten
Pascal P.
  • Mod
  • Blogger
  • Forum-Beiträge: 10.165

03.04.2014, 17:49:26 via Website

Eigentlich müsste es funktionieren.
Schon mal nen Project Clean versucht?

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
  • Forum-Beiträge: 1.525

03.04.2014, 17:50:32 via Website

Naja ich hoffe die "XXX" haste nicht umsonst gemacht :D.



LG

Antworten
Pascal P.
  • Mod
  • Blogger
  • Forum-Beiträge: 10.165

03.04.2014, 17:51:25 via Website

Das ist dich nur weil er keine Urls posten darf oder?

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
  • Forum-Beiträge: 1.525

03.04.2014, 17:56:31 via Website

Ja ok stimmt :D.

Dachte schon. Nicht das es sonst daran gelegen hätte. :D

Antworten
  • Forum-Beiträge: 16

03.04.2014, 18:06:25 via Website

Das Problem liegt in diesem Bereich. Wenn ich es lösche, bekomme ich kein Error.

DisplayMessage.java unter onCreate
1Intent intent = getIntent();
2String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
3
4TextView textView = new TextView(this);
5textView.setTextSize(40);
6textView.setText(message);
7
8setContentView(textView);

— geändert am 03.04.2014, 18:06:49

Antworten
  • Forum-Beiträge: 1.525

03.04.2014, 18:10:27 via Website

Was genau soll der Code tun deiner Meinung nach.
Sag mir bitte mal was deine App tun soll generell.



LG

Antworten
Pascal P.
  • Mod
  • Blogger
  • Forum-Beiträge: 10.165

03.04.2014, 18:12:42 via Website

Jo is klar was macht dein Code denn.


Es wird eine TextView generiert und diese Dann ins layout gesetzt. Willst du nicht die TextView auf deinem Fragment layout nehmen, anstatt als komplettes layout die TV zu setzen?

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
  • Forum-Beiträge: 1.525

03.04.2014, 18:15:54 via Website

Pascal P.
Jo is klar was macht dein Code denn.


Es wird eine TextView generiert und diese Dann ins layout gesetzt. Willst du nicht die TextView auf deinem Fragment layout nehmen, anstatt als komplettes layout die TV zu setzen?

Richtig. Lieber so.

Aber warum setzt du nicht gleich ein TextView ins Layout und findest es per "findViewById" ??
Und dann setzt du den Text.
Ist doch alles viel einfacher.



LG

— geändert am 03.04.2014, 18:16:28

Antworten
  • Forum-Beiträge: 1.525

03.04.2014, 18:23:02 via Website

Das ist ok.:D

Aber bitte nicht nur Copy&Paste, sondern lesen, lernen und verstehen.:D

Ich sehe gleich den ersten Fehler bei dir, was daran liegen könnte.
Schaue dir das nochmal durch.




LG

— geändert am 03.04.2014, 18:32:30

Antworten
  • Forum-Beiträge: 16

03.04.2014, 18:38:11 via Website

Ich kopiere nicht, ich hab wirklich so ungefähr 10 mal gelesen.

Antworten
  • Forum-Beiträge: 1.525

03.04.2014, 18:41:58 via Website

Sicher???:P

Für deine DisplayMessageActivity
1Note: If you are using an IDE other than Eclipse, your project does not contain the activity_display_message layout that's requested by setContentView(). That's OK because you will update this method later and won't be using that layout.


LG

— geändert am 03.04.2014, 18:42:12

Antworten
  • Forum-Beiträge: 16

03.04.2014, 18:48:03 via Website

Ich benutze ja eclipse und ich habe die activity_display_message

Antworten

Empfohlene Artikel