有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法实例化安卓编程类

我在这里观看了教程视频:http://www.youtube.com/watch?v=pZaRNVwKAy4&list=PLB03EA9545DD188C3&index=7,在观看教程时遇到了一些错误。在我开始学习教程:onCheckedChanged方法之前,一切都很顺利。有人能解释一下我做错了什么吗?非常感谢

LogCat代码

09-15 18:38:22.444: E/MediaPlayer(1764): Should have subtitle controller already set
09-15 18:38:27.984: E/MediaPlayer(1764): Should have subtitle controller already set
09-15 18:38:31.374: E/AndroidRuntime(1764): FATAL EXCEPTION: main
09-15 18:38:31.374: E/AndroidRuntime(1764): Process: com.example.myfirstapp, PID: 1764
09-15 18:38:31.374: E/AndroidRuntime(1764): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.TutorialOne}: java.lang.InstantiationException: can't instantiate class com.example.myfirstapp.TutorialOne
09-15 18:38:31.374: E/AndroidRuntime(1764):     at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
09-15 18:38:31.374: E/AndroidRuntime(1764):     at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-15 18:38:31.374: E/AndroidRuntime(1764):     at 安卓.app.ActivityThread.access$800(ActivityThread.java:135)
09-15 18:38:31.374: E/AndroidRuntime(1764):     at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-15 18:38:31.374: E/AndroidRuntime(1764):     at 安卓.os.Handler.dispatchMessage(Handler.java:102)
09-15 18:38:31.374: E/AndroidRuntime(1764):     at 安卓.os.Looper.loop(Looper.java:136)
09-15 18:38:31.374: E/AndroidRuntime(1764):     at 安卓.app.ActivityThread.main(ActivityThread.java:5017)
09-15 18:38:31.374: E/AndroidRuntime(1764):     at java.lang.reflect.Method.invokeNative(Native Method)
09-15 18:38:31.374: E/AndroidRuntime(1764):     at java.lang.reflect.Method.invoke(Method.java:515)
09-15 18:38:31.374: E/AndroidRuntime(1764):     at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-15 18:38:31.374: E/AndroidRuntime(1764):     at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-15 18:38:31.374: E/AndroidRuntime(1764):     at dalvik.system.NativeStart.main(Native Method)
09-15 18:38:31.374: E/AndroidRuntime(1764): Caused by: java.lang.InstantiationException: can't instantiate class com.example.myfirstapp.TutorialOne
09-15 18:38:31.374: E/AndroidRuntime(1764):     at java.lang.Class.newInstanceImpl(Native Method)
09-15 18:38:31.374: E/AndroidRuntime(1764):     at java.lang.Class.newInstance(Class.java:1208)
09-15 18:38:31.374: E/AndroidRuntime(1764):     at 安卓.app.Instrumentation.newActivity(Instrumentation.java:1061)
09-15 18:38:31.374: E/AndroidRuntime(1764):     at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
09-15 18:38:31.374: E/AndroidRuntime(1764):     ... 11 more

主Java代码

package com.example.myfirstapp;

import 安卓.app.Activity;
import 安卓.content.Intent;
import 安卓.media.MediaPlayer;
import 安卓.os.Bundle;


public class Main extends Activity {
// declare variables for the who class 
MediaPlayer logoMusic; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    // start music command 
    logoMusic=MediaPlayer.create(Main.this, R.raw.melody);
    logoMusic.start();

    // start thread 
    Thread logoTimer=new Thread(){
        public void run(){
            try {
                sleep(5000); // 1k is 1sec 
                Intent menuIntent=new Intent("com.example.myfirstapp.MENU");// address in   manifest 
                startActivity(menuIntent);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            finally
            {
                finish();
            }

        }

    };
    logoTimer.start();

}

@Override
protected void onPause() {
    super.onPause();

    logoMusic.release();

}

}

菜单Java代码

package com.example.myfirstapp;

import 安卓.app.Activity;
import 安卓.content.Intent;
import 安卓.media.MediaPlayer;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;



public class Menu extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final MediaPlayer buttonSound=MediaPlayer.create(Menu.this, R.raw.button);// make sure it is a final variable bc u are using it in sub 

    // set up buttons reference
    Button tut1=(Button) findViewById(R.id.tutorial1);
    Button tut2=(Button) findViewById(R.id.tutorial2);

    // setting up the button functions
    tut1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            buttonSound.start();

            startActivity(new Intent("com.example.myfirstapp.TutorialOne"));// short cut in  creating the intent 
        }
    });

     tut2.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
            buttonSound.start();
            startActivity(new Intent("com.example.myfirstapp.TutorialOne"));
        }


    });
    }

    @Override
    protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

}

java代码教程

package com.example.myfirstapp;

import 安卓.annotation.SuppressLint;
import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.view.Gravity;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.CompoundButton;
import 安卓.widget.CompoundButton.OnCheckedChangeListener;
import 安卓.widget.EditText;
import 安卓.widget.RadioGroup;
import 安卓.widget.TextView;

@SuppressLint("RtlHardcoded")
public abstract class TutorialOne extends Activity implements OnCheckedChangeListener{

// set up variables 
TextView textOut;
EditText textIn; 
RadioGroup gravityG,styleG;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tutorial1);
    //similar to how you set the button to reference to xml 
    textOut=(TextView)findViewById(R.id.tvChange);
    textIn=(EditText)findViewById(R.id.editText1);
    gravityG=(RadioGroup)findViewById(R.id.rgGravity);
    styleG=(RadioGroup)findViewById(R.id.rgStyle);

    Button gen=(Button)findViewById(R.id.generate);
    gen.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // when you click generate, it will text out 

            textOut.setText(textIn.getText());// get user input and output
        }
    });

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

public void onCheckedChanged(CompoundButton buttonView, int isChecked) {
    // TODO Auto-generated method stub
    switch(isChecked){
    case R.id.rbLeft:
        textOut.setGravity(Gravity.LEFT);// set gravity to left 
        break;
    case R.id.rbCenter:
        textOut.setGravity(Gravity.CENTER);
        break;
    case R.id.rbRight:
        textOut.setGravity(Gravity.RIGHT);
        break;
}
}}

梅因。xml

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myfirstapp.Main" >

<Button
    安卓:id="@+id/tutorial1"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="@string/button1" />

<Button
    安卓:id="@+id/tutorial2"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="31dp"
    安卓:text="@string/button2" />

</RelativeLayout>

飞溅。xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical" 
安卓:background="@drawable/flash">


</LinearLayout>

教程1。xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical" >

<EditText
    安卓:id="@+id/editText1"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:ems="10"
    安卓:text=""
    安卓:textStyle="bold" >

    <requestFocus />
</EditText>

<LinearLayout
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:orientation="horizontal"
    安卓:weightSum="2" >

    <TextView
        安卓:id="@+id/tvStyle"
        安卓:layout_width="fill_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_weight="1"
        安卓:text="@string/style"
        安卓:gravity="center" />

    <TextView
        安卓:id="@+id/tvGravity"
        安卓:layout_width="fill_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_weight="1"
        安卓:gravity="center"
        安卓:text="@string/gravity" />
</LinearLayout>

<LinearLayout
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:orientation="horizontal"
    安卓:weightSum="2" >

    <RadioGroup
        安卓:layout_width="fill_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_weight="1"
        安卓:orientation="vertical"
        安卓:id="@+id/rgStyle" >

        <RadioButton
            安卓:id="@+id/rbNormal"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="@string/normal" />

        <RadioButton
            安卓:id="@+id/rbItalic"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="@string/italic" />

        <RadioButton
            安卓:id="@+id/rbBold"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="@string/bold" />
    </RadioGroup>

    <RadioGroup
        安卓:layout_width="fill_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_weight="1"
        安卓:orientation="vertical"
        安卓:id="@+id/rgGravity" >

        <RadioButton
            安卓:id="@+id/rbLeft"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="@string/left" />

        <RadioButton
            安卓:id="@+id/rbCenter"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="@string/center" />

        <RadioButton
            安卓:id="@+id/rbRight"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="@string/right" />
    </RadioGroup>
</LinearLayout>

<TextView
    安卓:id="@+id/tvChange"
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:gravity="center"
    安卓:text="@string/text_view_change"
    安卓:textStyle="bold" />

<Button
    安卓:id="@+id/generate"
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:text="@string/generate"
    安卓:textSize="25sp" />

</LinearLayout>

AndroidManifest代码

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

<uses-sdk
    安卓:minSdkVersion="8"
    安卓:targetSdkVersion="21" />

<application
    安卓:allowBackup="true"
    安卓:icon="@drawable/ic_launcher"
    安卓:label="@string/app_name"
    安卓:theme="@style/AppTheme" >
    <activity
        安卓:name=".Main"
        安卓:label="@string/app_name" >
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAIN" />
            <category 安卓:name="安卓.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

      <activity
        安卓:name=".Menu"
        安卓:label="@string/app_name" >
        <intent-filter>
            <action 安卓:name="com.example.myfirstapp.MENU" />
            <category 安卓:name="安卓.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

        <activity
        安卓:name=".TutorialOne"
        安卓:label="@string/app_name" >
        <intent-filter>
            <action 安卓:name= "com.example.myfirstapp.TutorialOne" />
            <category 安卓:name="安卓.intent.category.DEFAULT" />
        </intent-filter>
    </activity>



</application>

</manifest>

共 (0) 个答案