Ik weet dat deze fout miljoenen keren op het forum is verschenen, maar help me alsjeblieft te vinden wat ik heb gemist.
Ik probeer een eenvoudige, op tabbladen georiënteerde applicatie te doen, ik heb niet veel (behalve fouten)
1) mijn hoofdactiviteit is gebaseerd op de tablayout-tutorial die ik heb gevonden
public class MainTabPanel extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, MyBookActivity.class);
spec = tabHost.newTabSpec("main")
.setIndicator("Main", res.getDrawable(R.drawable.ic_mybook))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
2) mainlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout></TabHost>
3) mijn tweede activiteit is in principe bijna leeg, het geeft alleen de huidige datum en tijd weer, werkte voordat ik probeerde een tabblad toe te voegen
4) mijn manifestbestand
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.th.mybook"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainTabPanel"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="MyBookActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.ALTERNATIVE" />
</intent-filter>
</activity>
</application>
</manifest>
5 log cat-fout
02-10 21:04:45.203: E/AndroidRuntime(1107): FATAL EXCEPTION: main
02-10 21:04:45.203: E/AndroidRuntime(1107): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.th.mybook/org.th.mybook.MainTabPanel}: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.th.mybook/org.th.mybook.MyBookActivity}: java.lang.NullPointerException
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.os.Looper.loop(Looper.java:123)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-10 21:04:45.203: E/AndroidRuntime(1107): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 21:04:45.203: E/AndroidRuntime(1107): at java.lang.reflect.Method.invoke(Method.java:521)
02-10 21:04:45.203: E/AndroidRuntime(1107): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-10 21:04:45.203: E/AndroidRuntime(1107): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-10 21:04:45.203: E/AndroidRuntime(1107): at dalvik.system.NativeStart.main(Native Method)
02-10 21:04:45.203: E/AndroidRuntime(1107): Caused by: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.th.mybook/org.th.mybook.MyBookActivity}: java.lang.NullPointerException
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.widget.TabHost.addTab(TabHost.java:213)
02-10 21:04:45.203: E/AndroidRuntime(1107): at org.th.mybook.MainTabPanel.onCreate(MainTabPanel.java:30)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-10 21:04:45.203: E/AndroidRuntime(1107): ... 11 more
02-10 21:04:45.203: E/AndroidRuntime(1107): Caused by: java.lang.NullPointerException
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
02-10 21:04:45.203: E/AndroidRuntime(1107): at org.th.mybook.MyBookActivity.<init>(MyBookActivity.java:16)
02-10 21:04:45.203: E/AndroidRuntime(1107): at java.lang.Class.newInstanceImpl(Native Method)
02-10 21:04:45.203: E/AndroidRuntime(1107): at java.lang.Class.newInstance(Class.java:1429)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-10 21:04:45.203: E/AndroidRuntime(1107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
02-10 21:04:45.203: E/AndroidRuntime(1107): ... 20 more
help me alsjeblieft, en vertel me wat ik heb gemist, ik vergelijk deze code met mijn oude en ik kan niets vinden
groeten
6) mijn boekactiviteit
public class MyBookActivity extends Activity {
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DigitalClock clock = (DigitalClock) findViewById(R.id.digitalClock1);
final TextView date = (TextView) findViewById(R.id.textView1);
date.setText(dateFormat.format(new Date()));
TextWatcher watcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (s.toString().startsWith("00:00:00")
|| s.toString().startsWith("12:00:00")) {
date.setText(dateFormat.format(new Date()));
}
}
};
clock.addTextChangedListener(watcher);
}
}
7) lay-out main.xml -> voor mijn boekactiviteit
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/DatePanel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/space"
android:layout_weight="1"
android:text="TextView" />
<DigitalClock
android:id="@+id/digitalClock1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="DigitalClock" />
</LinearLayout>
</LinearLayout>
Antwoord 1, autoriteit 100%
<activity
android:name="MyBookActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.ALTERNATIVE" />
</intent-filter>
</activity>
waar is je stip voor MyBookActivity?
Antwoord 2, autoriteit 67%
Het was mijn eigen stommiteit:
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
Door dit in de onCreate()
-methode te plaatsen, werd mijn probleem opgelost.
Antwoord 3, autoriteit 58%
Ik had hetzelfde probleem, ik heb het project schoongemaakt en opnieuw opgebouwd en het werkte.
Antwoord 4, autoriteit 42%
Uw Manifest
moet zo veranderen Activiteitnaam moet worden opgegeven als ".YourActivityname"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.th.mybook"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8" android:targetSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainTabPanel"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MyBookActivity" >
</activity>
</application>
Antwoord 5, autoriteit 8%
Geachte, je hebt twee Intent-opstartprogramma’s gebruikt in je manifest. Maak slechts één activiteit als opstartprogramma:
Uw manifestactiviteit is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.th.mybook"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainTabPanel"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="MyBookActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.ALTERNATIVE" />
</intent-filter>
</activity>
</application>
</manifest>
schrijfcode is nu (ik heb van uw ‘MyActivityBook’ uw standaard activiteitenstarter gemaakt. Kopieer en plak deze in uw manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.th.mybook"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainTabPanel"
android:label="@string/app_name" >
</activity>
<activity
android:name="MyBookActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
en Tweede fout kan zijn als u oude code kopieert en plakt, werk dan com.example.packagename.FILE_NAME
bij
hoop dat dit zal werken!
Antwoord 6
Na een paar antwoorden geprobeerd te hebben, zijn ze ofwel niet gerelateerd aan mijn project of heb ik geprobeerd op te schonen en opnieuw op te bouwen (https://stackoverflow. com/a/48760966/8463813). Maar bij mij werkte het niet direct. Ik heb het vergeleken met een oudere versie van code, waarin ik heb gezien dat sommige bibliotheekbestanden (jars en aars in de directory External Libraries) ontbreken. Geprobeerd Cache ongeldig maken en opnieuw opstartenwerkte, waardoor alle bibliotheken werden gemaakt en goed werkte.
Antwoord 7
Mijn probleem leek op elkaar, na een paar uur ontdekte ik dat niet al mijn projecten waren geconverteerd naar Androidx, dus ik veranderde:
<android.support.constraint.ConstraintLayout
in
<androidx.constraintlayout.widget.ConstraintLayout
dan klaagt androidStudio nog steeds, maar de probleemoplosser kan een constraintlayout toevoegen aan je bibliotheken of zo.
Nu begint het opnieuw! Het is grappig dat de afgelopen 8 jaar veel vergelijkbare problemen zijn opgetreden met een andere oplossing.