【问题标题】:RuntimeException: Unable to start activity ComponentInfoRuntimeException:无法启动活动 ComponentInfo
【发布时间】:2018-12-07 14:45:57
【问题描述】:

这是我的 home_fragment.xml

<?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:background="#ffffff"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center_horizontal" >

        <ImageButton
            android:id="@+id/concerts"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/concerts_style"
            android:contentDescription="@string/concerts" />
        <!-- android:onClick="gotoConcertsActivity" -->

        <ImageButton
            android:id="@+id/cinema"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/cinema_style"
            android:contentDescription="@string/cinema" />
        <!-- android:onClick="gotoCinemaActivity" -->
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <ImageButton
            android:id="@+id/theater"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/theater_style"
            android:contentDescription="@string/theater" />
        <!-- android:onClick="gotoTheaterActivity" -->

        <ImageButton
            android:id="@+id/cultural_events"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/cultural_style"
            android:contentDescription="@string/cultural_events" />
        <!-- android:onClick="gotoCulturalEventsActivity" -->
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:gravity="center_horizontal" >

        <ImageButton
            android:id="@+id/food"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:background="@drawable/food_style"
            android:contentDescription="@string/food" />

        <!-- android:onClick="android:onClick="gotoFoodActivity"" -->

        <ImageButton
            android:id="@+id/sports"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/sports_style"
            android:contentDescription="@string/sports" />
        <!-- android:onClick="gotoSportsActivity"" -->
    </LinearLayout>
</LinearLayout>

这是 Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.larissaeventguide"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- Splash Activity -->
        <activity
            android:name="com.example.larissaeventguide.SplashScreenWelcome"
            android:label="@string/title_activity_splash_screen_welcome" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Main Activity -->

        <activity
            android:name="com.example.larissaeventguide.MainActivity"
            android:label="@string/app_name" >
        </activity>

        <!-- Concert Activity -->
        <activity android:name="com.example.larissaeventguide.ConcertsActivity" />

        <!-- Cinema Activity -->
        <activity android:name="com.example.larissaeventguide.CinemaActivity" />

        <!-- TheaterActivity -->
        <activity android:name="com.example.larissaeventguide.TheaterActivity" />

        <!-- Food Activity -->
        <activity android:name="com.example.larissaeventguide.FoodActivity" />

        <!-- CulturalEventsActivity -->
        <activity android:name="com.example.larissaeventguide.CulturalEventsActivity" />

        <!-- SportsActivity -->
        <activity android:name="com.example.larissaeventguide.SportsActivity" />

        <!-- Happens Now Activity -->
        <activity android:name="com.example.larissaeventguide.HappensNowActivity" />

        <!-- TabPagerAdapte -->
        <activity android:name=".TabPagerAdapter" />
    </application>

</manifest>

这是我的 MainActivity

package com.example.larissaeventguide;
import info.LarissaEventGuide.tabsswipe.adapter.TabsPagerAdapter;
import com.example.larissaeventguide.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private static final String LOGTAG = "MainActivity";
    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Home", "About", "Map" };
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(LOGTAG,"onCreate");

        //Buttons concerts
        ImageButton concerts = (ImageButton)findViewById(R.id.concerts);

        concerts.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent (MainActivity.this, ConcertsActivity.class);
                startActivity(intent);

            }
        });
        //Button cinema
        ImageButton cinema = (ImageButton)findViewById(R.id.cinema);
            cinema.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View v) {
                Intent intent = new Intent (MainActivity.this, CinemaActivity.class);
                startActivity(intent);
                }
        });

        //Button Theater
        ImageButton theater = (ImageButton)findViewById(R.id.theater);
            theater.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent (MainActivity.this,TheaterActivity.class);
                startActivity(intent);

            }
        });

        //Button cultural_events
        ImageButton cultural_events = (ImageButton)findViewById(R.id.cultural_events);
            cultural_events.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View v) {
                Intent intent = new Intent (MainActivity.this, CulturalEventsActivity.class);
                startActivity(intent);
                }
        });
            //Button Food
        ImageButton food = (ImageButton)findViewById(R.id.food);

        food.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View v) {
                Intent intent = new Intent (MainActivity.this, FoodActivity.class);
                startActivity(intent);
                }
        });
            //Button Sports
        ImageButton sports = (ImageButton)findViewById(R.id.sports);
            sports.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View v) {
                Intent intent = new Intent (MainActivity.this, SportsActivity.class);
                startActivity(intent);
                }
        });

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));}
    }
        @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
        @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }
        @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        }); 

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }       


    @Override
    protected void onResume() {
        super.onResume();
        Log.d(LOGTAG,"onResume");
        }
        @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.d(LOGTAG,"onStart");
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.d(LOGTAG,"onPause");
    }
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.d(LOGTAG,"onStop");
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.d(LOGTAG,"onDestroy");
    }
    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        Log.d(LOGTAG,"onRestart");
    }   
}

我得到了这个日志输出

05-13 13:16:17.507: E/AndroidRuntime(1303): FATAL EXCEPTION: main
05-13 13:16:17.507: E/AndroidRuntime(1303): Process: com.example.larissaeventguide, PID: 1303
05-13 13:16:17.507: E/AndroidRuntime(1303): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.larissaeventguide/com.example.larissaeventguide.MainActivity}: java.lang.NullPointerException
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.os.Looper.loop(Looper.java:136)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at java.lang.reflect.Method.invoke(Method.java:515)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at dalvik.system.NativeStart.main(Native Method)
05-13 13:16:17.507: E/AndroidRuntime(1303): Caused by: java.lang.NullPointerException
05-13 13:16:17.507: E/AndroidRuntime(1303):     at com.example.larissaeventguide.MainActivity.onCreate(MainActivity.java:46)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.Activity.performCreate(Activity.java:5231)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-13 13:16:17.507: E/AndroidRuntime(1303):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-13 13:16:17.507: E/AndroidRuntime(1303):     ... 11 more

我是 Android 编程新手,所以我遵循一些手册。

制作图像按钮后,eclipse 向我发送此错误,我不确定发生了什么

【问题讨论】:

  • 您在第 46 行访问一个空对象;自那次运行以来,您的行号已损坏,但可能是 Concerts.setOnClickListener 行。很可能,在您的布局中找不到您的 ImageButton - 也许它在不同的布局中。
  • 但是为什么?我的意思是这是我唯一拥有这些按钮的地方。
  • 看起来您的按钮位于 home_fragment.xml 中,但您已将内容视图设置为 activity_main.xml
  • 我试图改变它,但不幸的是它没有工作
  • “不幸的是它没有用”不是问题报告,我们可以为您提供帮助

标签: android exception


【解决方案1】:

发生这种情况是因为您将main.xmlfragment.xml 混合在一起。我认为您首先不知道如何使用片段,您看到this 可能会有所帮助

【讨论】:

    【解决方案2】:

    看起来你正在混淆你的片段和你的活动。 在 onCreate 中,您将布局设置为 R.layout.activity_main,但您尝试从 home_fragment.xml 获取视图。您似乎没有在活动中添加片段,因此这可能是您的问题。

    您是否打算添加一个包含您在上面发布的布局的主页片段?在这种情况下,您必须获取视图并在片段本身中分配点击侦听器。 如果您不打算添加主页片段,则必须调整您的 activity_main 布局并在那里添加按钮。

    【讨论】:

      【解决方案3】:

      05-13 13:16:17.507:E/AndroidRuntime(1303):java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.larissaeventguide/com.example.larissaeventguide.MainActivity}:java.lang。空指针异常........

      出现错误是因为在“MainActivity”中,您将内容视图设置为“activity_main.xml”并定义了 home_fragment.xml 的字段。如果你想在你的活动中使用片段布局,那么用它的布局定义一个片段类并像这样膨胀它:

      在你的activity.xml文件中定义一个布局:

              <?xml version="1.0" encoding="utf-8"?>
              <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:app="http://schemas.android.com/apk/res-auto"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="@drawable/drawable_gradient_animation_list"
                  android:id="@+id/spinning_wheel_image"
                  tools:context=".MainActivity">
      
                  <FrameLayout
                      android:id="@+id/container"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent">
      
                  </FrameLayout>
      
              </android.support.constraint.ConstraintLayout>
      

      并像这样定义一个片段类 HomeFragment:

      public class HomeFragment extends Fragment {
      
      
           @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
              // Inflate the layout for this fragment
              View view = inflater.inflate(R.layout.home_fragment, container, false);
      
              return view;
      
      }
      
      
      }
      

      并在您的 MainActivity 的 onCreate() 方法中使用它来膨胀您的“home_fragment.xml”,如下所示:

                   Fragment  fragment = new HomeFragment();
                   android.support.v4.app.FragmentTransaction fragmentTransaction = 
                   getSupportFragmentManager().beginTransaction();
                  fragmentTransaction.replace(R.id.container, fragment).commit();
      

      它会解决你的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-23
        • 1970-01-01
        相关资源
        最近更新 更多