当我尝试运行我的应用程序时,我收到此错误我认为我的menubutton导致此错误但不知道如何解决问题
mainactivity
package com.example.vitoriano_vaz.eastbayvapes;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import java.util.List;
/*
This app is for a local Vape Shop
it's purpose is to increase customer flow, and provide a menu for Android users
the array is going to be called = da_menu
*/
public class MainActivity extends AppCompatActivity {
private static Intent java;
//used this to get rid of red line under intent on line 38 if it causes more problems remove and resolve issue
private Intent intent;
/*
called when the user clicks the send Button
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
attempting to send user to each individual app with the URI Intent method
*/
PackageManager packageManager = getPackageManager();
List activites = packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafe = activites.size() > 0;
Uri number = Uri.parse("tel:9259619029");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
Uri webpage = Uri.parse("http://www.facebook.com/eastbayvapes");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
/*this is to send the user to facebook application
try {
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/eastbay"));
} catch (Exception e) {
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/eastbay"));
}
*/
// intent to send user to instagram
Uri uri = Uri.parse("http://instagram.com/_u/eastbayvapes");
Intent likeIng = new Intent(Intent.ACTION_VIEW, uri);
likeIng.setPackage("com.instagram.android");
try {
startActivity(likeIng);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://instagram.com/eastbayvapes")));
}
//Intent to send user to email app
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"eastbayvapes@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "");
startActivity(Intent.createChooser(intent, ""));
/*Intent to send user to google maps
String uri = "https://maps.google.co.in/maps?q=" + 1549 N Vasco Rd, Livermore, Ca 94551;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(intent);
*/
/*
sends user to about us xml via AboutActivity.class
*/
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
}
});
}
}
SecondActivity 在这里,我相信当我宣布menubutton但我不知道我做错了什么时,我得到了错误的原因
package com.example.vitoriano_vaz.eastbayvapes;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
/**
* Created by vitoriano-vaz on 5/11/16.
*/
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menuscreen);
/*
this is the button call that sends user to the menuscreen.xml
*/
Button mbutton = (Button) findViewById(R.id.menubutton);
mbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
populateListView();
}
});
populateListView();
}
private void populateListView() {
// Create the list of items
String[] juiceMenu= {
"#1 BLUEBERRY BOMBSHELL \nThis blueberry dessert will keep your taste buds begging for more",
"#2 RICHIE RICH\nShout out to Rich! This was his all day vape. Strawberry cream with a bunch of deliciousness",
"#3 CHIQUITA\nCreamy Banana with a bready after taste",
"#4 AFTERNOON DELIGHT\nGot that 3 o'clock feeling? Try a little Afternoon delight. Kiwi Strawberry Dessert",
"#5 POPPIN OTTERS\nMelt an otterpop put it in your tank and vape it",
"#6 VIVA LA SANGRIA\nFruit, with a little bit of fruit, add a teaspoon of fruit mixed with white win and top it off with fruit",
"#7 OKOLE MALUNA\nPineapple fruit blend. Bottom up!",
"#8 CARMEN MIRANDA\nVanilla custard with a crisp",
"#9 POMALADE\nIt's Bombalade! Yuour mother will love it",
"#10 IZUAL\nWhirlwind of icy Fruits",
"#11 BUTTER STOTCH\nProfessor Chaos will surely get grounded for this one!",
"#12 BLUE BULLS\nGrab the bull by the horns",
"#13 GRAPE APE\nA grapple a day will keep the doctor away",
"#14 BRUCE JUICE\nPerfectly blended old fashioned. This one is for you Bruce",
"#15 DOC HOLIDAY\nN.E.T. Tabacco flavor. Limited release",
"#16 PEACHY KEEN\nPeach cobbler. Better than what your mama used to make",
"#17 HULA\nStrawberry coconut? What in the hula?",
"#18 NEW YORK\nPeppermint paddy dipped in chocolate...Amazing really",
"#19 AL GORE\nIm super duper cereal right now!",
"#20 LUX CHARMS\nHearts, stars, horseshoes, clovers & blue moons, pots of gold % rainbows & me red balloons",
"#21 SAILOR JACK\nCant go to the ball game without some cracker jacks",
"#22 GET HIM TO THE GREEK\n...Yogurt, peach, delightful",
"#23 KEY WE LIE CHI\nFruit lovers will enjoy this lychee vape",
"#24 SPRING FLING\nA menage a trois of honeydew and cream on your tongue",
"#25 GUMBY\nWatermelon sour gummy",
"#26 CHAI\nMILK and Chai Tea",
"#27 MR. BBEAN\nCappuccino with cream to start the day off right",
"#28 50 SHADES OF ORANGE\nA delightful blend of orange and cream",
"#29 BLUE WAFFLES\nBreakfast for dinner with this Blueberry Waffle",
"#30 ENIGMA\nEnigma conundrum of mango raspberry with a hint of mystery can you solve the puzzle?",
"#31 MR. FREEZE\nPut down the tank and STEP AWAY hardcore mint lovers ONLY! This juice is too coll for most",
"#32 CUP OF JOE\nThe best part of waking up is coffee in your vape",
"#33 BON JOVI\nVanilla Bourbon ",
};
// Build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, // context for the activity
R.layout.da_menu, // Layout to create
juiceMenu); // Items to be displayed
//configure list view
ListView list = (ListView) findViewById(R.id.listViewMain);
list.setAdapter(adapter);
}
}
AndroidManifest
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".AboutActivity"
android:screenOrientation="portrait"/>
<activity android:name=".SecondActivity"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:src="@drawable/ebvbottle"
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="com.example.vitoriano_vaz.eastbayvapes.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="East Bay Vapes"
android:textAllCaps="true"
android:textColor="#FEFEFF"
android:textSize="28sp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="About Us"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu"
android:id="@+id/menubutton"
android:layout_alignTop="@+id/button"
android:layout_alignParentEnd="true"
android:onClick="menubutton" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="We are dedicated to helping people get off tabacco! We carry everything from beginner setups, to advanced personalized vaporizers. We pride ourselves in our premuim juices made in shop!"
android:id="@+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="22dp"
android:typeface="serif" />
</RelativeLayout>
menuscreen 这是我要发送填充数组的地方
<?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:scrollIndicators="right">
<ListView
android:id="@+id/listViewMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</RelativeLayout>
这是我的错误
05-18 16:00:44.619 11983-11983/com.example.vitoriano_vaz.eastbayvapes E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vitoriano_vaz.eastbayvapes, PID: 11983
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vitoriano_vaz.eastbayvapes/com.example.vitoriano_vaz.eastbayvapes.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.resolveTypeIfNeeded(android.content.ContentResolver)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2622)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2683)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:6066)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.resolveTypeIfNeeded(android.content.ContentResolver)' on a null object reference
at android.app.ApplicationPackageManager.queryIntentActivitiesAsUser(ApplicationPackageManager.java:808)
at android.app.ApplicationPackageManager.queryIntentActivities(ApplicationPackageManager.java:797)
at com.example.vitoriano_vaz.eastbayvapes.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:6584)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2575)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2683)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:6066)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
答案 0 :(得分:0)
初始化你的意图,使其不是空的。
List activites = packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
修改强>
从代码中删除以下部分,因为它在其余代码中没有用处。在你意识到为什么需要它们之前,永远不要使用它们。
PackageManager packageManager = getPackageManager();
List activites = packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafe = activites.size() > 0;